【发布时间】:2018-08-14 07:53:27
【问题描述】:
我有一个使用 ActiveMQ 的 JMS Producer/Subscriber 的简单 Spring 应用程序,配置如下:
应用程序上下文 xml:
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616" />
<property name="userName" value="user" />
<property name="password" value="password" />
</bean>
<bean id="messageDestination" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="messageQueue1" />
</bean>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="sessionAcknowledgeModeName" value="CLIENT_ACKNOWLEDGE">
</property>
</bean>
<bean id="springJmsProducer" class="SpringJmsProducer">
<property name="destination" ref="messageDestination" />
<property name="jmsTemplate" ref="jmsTemplate" />
</bean>
<bean id="springJmsConsumer" class="SpringJmsConsumer">
<property name="destination" ref="messageDestination" />
<property name="jmsTemplate" ref="jmsTemplate" />
</bean>
下面是Spring生产者
public class SpringJmsProducer {
private JmsTemplate jmsTemplate;
private Destination destination;
public JmsTemplate getJmsTemplate() {
return jmsTemplate;
}
public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
public Destination getDestination() {
return destination;
}
public void setDestination(Destination destination) {
this.destination = destination;
}
public void sendMessage(final String msg) {
jmsTemplate.send(destination, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(msg);
}});
}
}
下面是 Spring Consumer:
public class SpringJmsConsumer {
private JmsTemplate jmsTemplate;
private Destination destination;
public JmsTemplate getJmsTemplate() {
return jmsTemplate;
}
public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
public Destination getDestination() {
return destination;
}
public void setDestination(Destination destination) {
this.destination = destination;
}
public String receiveMessage() throws JMSException {
TextMessage textMessage =(TextMessage) jmsTemplate.receive(destination);
return textMessage.getText();
}
}
问题:当我启动生产者并发布消息,然后我启动消费者时,消费者不是在阅读旧消息,而是只阅读消费者启动后发布的消息。谁能帮助我如何制作这个持久订阅者,以便消费者读取队列中未确认的消息,并且我需要实现同步消费者而不是异步。
我已经尝试了所有可能的解决方案,但没有一个有效。任何帮助都非常感谢
【问题讨论】:
-
我还需要实现同步消费者而不是异步。 ??当然 ?或相反,因为您的实际消费者是同步的
-
是的同步,即我提到同步,因为我在谷歌上找到的大多数解决方案都是异步的。
-
查看我的更新答案
-
我已经尝试过您的更新答案以及第一点,但仍然面临同样的问题。不知道确切的问题在哪里。任何其他方法或修复?
标签: spring jms activemq jmstemplate