Java 新创建的客户端是否知道hornetq中的旧消息?
我在hornetQ中创建了一个消费者会话,然后使用producer在队列中添加了4条消息。在此之后,我创建了新的消费者。Java 新创建的客户端是否知道hornetq中的旧消息?,java,jms,hornetq,Java,Jms,Hornetq,我在hornetQ中创建了一个消费者会话,然后使用producer在队列中添加了4条消息。在此之后,我创建了新的消费者。该消费者是否知道旧消息 如果没有,是否可以在XML中配置它 我创建了一个新的消费者,它无法获取以前的消息。我只是想确认一下这种行为是否正确?我在文档中找不到任何帮助。 下面是代码片段: TextMessage receivedMessage = (TextMessage)consumer.receive(); receivedMessage.acknowledge(); Sy
该消费者是否知道旧消息
如果没有,是否可以在XML中配置它 我创建了一个新的消费者,它无法获取以前的消息。我只是想确认一下这种行为是否正确?我在文档中找不到任何帮助。
下面是代码片段:
TextMessage receivedMessage = (TextMessage)consumer.receive();
receivedMessage.acknowledge();
System.out.println("Got order: " + receivedMessage.getText());
//consumer.close();
MessageConsumer newConsumer = session.createConsumer(orderQueue);
receivedMessage = (TextMessage)newConsumer.receive();
receivedMessage.acknowledge();
System.out.println("Got order: " + receivedMessage.getText());
如果我取消对consumer.close()行的注释,它可以正常工作。
My hornetq-jms.xml
<connection-factory name="NettyConnectionFactory">
<xa>true</xa>
<connectors>
<connector-ref connector-name="netty"/>
</connectors>
<entries>
<entry name="/XAConnectionFactory"/>
</entries>
<consumer-window-size>0</consumer-window-size>
</connection-factory>
<connection-factory name="NettyConnectionFactory">
<xa>false</xa>
<connectors>
<connector-ref connector-name="netty"/>
</connectors>
<entries>
<entry name="/ConnectionFactory"/>
</entries>
<consumer-window-size>0</consumer-window-size>
</connection-factory>
<connection-factory name="NettyThroughputConnectionFactory">
<xa>true</xa>
<connectors>
<connector-ref connector-name="netty-throughput"/>
</connectors>
<entries>
<entry name="/XAThroughputConnectionFactory"/>
</entries>
<consumer-window-size>0</consumer-window-size>
</connection-factory>
<connection-factory name="NettyThroughputConnectionFactory">
<xa>false</xa>
<connectors>
<connector-ref connector-name="netty-throughput"/>
</connectors>
<entries>
<entry name="/ThroughputConnectionFactory"/>
</entries>
<consumer-window-size>0</consumer-window-size>
</connection-factory>
getTransportConfiguration()的代码:
private synchronized static TransportConfiguration getTransportConfiguration(){
HashMap transportConfigurationMap=新的HashMap();
TransportConfiguration tc=transportConfigurationMap.get(“machinename:5455”);
如果(tc==null){
Map connectionParams=new HashMap();
connectionParams.put(org.hornetq.core.remoting.impl.netty.TransportConstants.HOST_PROP_NAME,“machinename”);
connectionParams.put(org.hornetq.core.remoting.impl.netty.TransportConstants.PORT_PROP_NAME,Integer.valueOf(“5455”);
tc=新的传输配置(NettyConnectorFactory.class.getName(),connectionParams);
transportConfigurationMap.put(“机器名称:5455”,tc);
}
返回tc;
您正在寻找的功能是通过持久订阅提供的。这是标准JMS规范的一部分。我相信,如果您查看您版本的HornetQ文档,您会找到它。此外,这里还有一个在HornetQ中使用JMS持久订阅的功能。是的,它会知道您的旧消息。但是在这个案例中,您有自己的旧的使用者仍处于打开状态,因此使用者将在其缓冲区中缓存消息,除非您关闭它,或者更改使用者窗口大小=0
大多数消息系统将在消费者上提前缓存,因此下次在消费者上调用receive时,消息将准备好接收
但是,如果您的消费者速度慢,并且您没有那么多的消息,则在您关闭该消费者之前,该消息将位于客户端的缓冲区中
对于生产中的快速消费者,最好总是提前缓存,因为这样可以提高吞吐量,而不缓存时,吞吐量会受到网络延迟的限制
在HornetQ案例中,您可以通过将消费者窗口大小设置为0来应对缓慢的消费者
在通过JNDI查找实例化连接工厂的情况下:
<connection-factory name="ConnectionFactory">
<connectors>
<connector-ref connector-name="netty-connector"/>
</connectors>
<entries>
<entry name="ConnectionFactory"/>
</entries>
<!-- We set the consumer window size to 0, which means messages are not buffered at all
on the client side -->
<consumer-window-size>0</consumer-window-size>
</connection-factory>
0
或者在直接实例化连接工厂的情况下,必须在实例中设置consumerWindowSize:
TransportConfiguration transportConfiguration = new
TransportConfiguration(NettyConnectorFactory.class.getName());
HornetQConnectionFactory cf =
HornetQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF,getTransportConfiguration());
cf.setConsumerWindowSize(0) // <<<<<< here
TransportConfiguration TransportConfiguration=新建
传输配置(NettyConnectorFactory.class.getName());
HornetQConnectionFactoryCF=
HornetQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF,getTransportConfiguration());
cf.setConsumerWindowSize(0)//mattgemell.com/2008/12/08/您尝试过什么//@Greg Kopff:我尝试过,但我的消费者无法获得以前的消息。我只是想确认此行为是否正确?我在文档中找不到任何帮助。您应该提供一些代码来更清楚地说明您是什么doing@Clebert:我已为添加了代码段r更多信息。感谢您的回答。但是,如果我没有关闭旧消费者,新消费者将不会收到旧消息:(看看我的答案……消息被缓存在消费者的缓冲区。我还为您提供了一个指向文档的链接。在查看文档后,我认为HornetQ没有实现追溯消费者。与ActiveMQ或IBM MQSeries不同……马扎尼卡:那是错的……用户只是让缓存在消费者缓冲区上。ActiveMQ具有cache-a头,我也相信MQSeries。这里没有关于追溯消费者的内容。这是一个队列。我不确定我是否理解您的评论。问题是:“…我使用producer在队列中添加了4条消息。在此之后,我创建了新的消费者。这个消费者会知道旧消息吗?”这就是所谓的追溯消费。我看不到一个方法来实现它使用大黄蜂。请张贴一个代码,这样做。
<connection-factory name="ConnectionFactory">
<connectors>
<connector-ref connector-name="netty-connector"/>
</connectors>
<entries>
<entry name="ConnectionFactory"/>
</entries>
<!-- We set the consumer window size to 0, which means messages are not buffered at all
on the client side -->
<consumer-window-size>0</consumer-window-size>
</connection-factory>
TransportConfiguration transportConfiguration = new
TransportConfiguration(NettyConnectorFactory.class.getName());
HornetQConnectionFactory cf =
HornetQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF,getTransportConfiguration());
cf.setConsumerWindowSize(0) // <<<<<< here
// Step 5. Create a JMS Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Step 6. Create a JMS Message Producer
MessageProducer producer = session.createProducer(queue);
// Step 7. Create a JMS MessageConsumer
MessageConsumer consumer1 = session.createConsumer(queue);
// Step 8. Start the connection
connection.start();
// Step 9. Send 10 messages to the queue
final int numMessages = 10;
for (int i = 0; i < numMessages; i++)
{
TextMessage message = session.createTextMessage("This is text message: " + i);
producer.send(message);
}
System.out.println("Sent messages");
// Step 10. Create another consumer on the same queue
MessageConsumer consumer2 = session.createConsumer(queue);
// Step 11. Consume three messages from consumer2
for (int i = 0; i < 3; i++)
{
TextMessage message = (TextMessage)consumer2.receive(2000);
System.out.println("Consumed message from consumer2: " + message.getText());
}