本文介绍了重启后HornetQ不会保留消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用HornetQ作为队列提供者,因为它具有持久性功能。但是,重新启动应用程序后,队列中的所有消息都将丢失。可能是配置问题?

I am using HornetQ as a queue provider as it has persistence capabilities. However, after I restart the application, all the messages in the queue are lost. Maybe configuration issues?

以下是代码:

// Step 1. Create the Configuration, and set the properties accordingly
Configuration configuration = new ConfigurationImpl();
configuration.setPersistenceEnabled(true);
configuration.setPersistIDCache(true);
configuration.setJMXManagementEnabled(true);
configuration.setMessageCounterEnabled(true);
configuration.setPersistDeliveryCountBeforeDelivery(true);
configuration.setSecurityEnabled(false);
configuration.setJournalSyncTransactional(true);
configuration.setJournalSyncNonTransactional(true);
configuration.setJournalType(JournalType.NIO);

HashSet<TransportConfiguration> transports = new HashSet<TransportConfiguration>();

transports.add(new TransportConfiguration(NettyAcceptorFactory.class.getName()));
transports.add(new TransportConfiguration(InVMAcceptorFactory.class.getName()));
    configuration.setAcceptorConfigurations(transports);

// Step 2. Create and start the server
eServer = new EmbeddedHornetQ();
eServer.setConfiguration(configuration);
eServer.start();

// Step 3. As we are not using a JNDI environment we instantiate the objects irectly
ServerLocator serverLocator = HornetQClient.createServerLocatorWithHA(new TransportConfiguration(NettyConnectorFactory.class.getName()), new TransportConfiguration(InVMConnectorFactory.class.getName()));
sessionFactory = serverLocator.createSessionFactory();

// Step 4. Create a core queue
coreSession = sessionFactory.createSession(true, true, true);
QueueQuery qq = coreSession.queueQuery(SimpleString.toSimpleString(QUEUE_NAME));
if(!qq.isExists()) coreSession.createQueue(QUEUE_NAME, QUEUE_NAME, true);
coreSession.close();

// Step 5. Create the session, and the producer
session = sessionFactory.createSession();
producer = session.createProducer(QUEUE_NAME);

// Step 6. Create the message consumer and set a listener
ClientConsumer messageConsumer = session.createConsumer(QUEUE_NAME);
messageConsumer.setMessageHandler(new QueueListener(session, producer));

// Step 7. Start the connection.
session.start();

// ...

// put 50,000 messages into the queue

// ...

session.close();
sessionFactory.close();
eServer.stop();

我还尝试打印日志文件,但消息计数始终为0:

I have also tried to print the journal files, but the message count is always 0:

JournalStorageManager.describeMessagesJournal(journalDirectory);





#JournalFileImpl: (hornetq-data-1.hq id = 209, recordID = 209)
#JournalFileImpl: (hornetq-data-2.hq id = 210, recordID = 210)
#JournalFileImpl: (hornetq-data-3.hq id = 211, recordID = 211)
#JournalFileImpl: (hornetq-data-4.hq id = 212, recordID = 212)
#JournalFileImpl: (hornetq-data-9.hq id = 213, recordID = 213)
#JournalFileImpl: (hornetq-data-10.hq id = 214, recordID = 214)
#JournalFileImpl: (hornetq-data-17.hq id = 215, recordID = 215)
#JournalFileImpl: (hornetq-data-18.hq id = 216, recordID = 216)
#JournalFileImpl: (hornetq-data-35.hq id = 217, recordID = 217)
#JournalFileImpl: (hornetq-data-36.hq id = 218, recordID = 218)
#JournalFileImpl: (hornetq-data-57.hq id = 219, recordID = 219)
#JournalFileImpl: (hornetq-data-58.hq id = 220, recordID = 220)
#JournalFileImpl: (hornetq-data-71.hq id = 221, recordID = 221)
#JournalFileImpl: (hornetq-data-72.hq id = 222, recordID = 222)
#JournalFileImpl: (hornetq-data-73.hq id = 223, recordID = 223)
#JournalFileImpl: (hornetq-data-74.hq id = 224, recordID = 224)
#JournalFileImpl: (hornetq-data-187.hq id = 225, recordID = 225)
#JournalFileImpl: (hornetq-data-188.hq id = 226, recordID = 226)
#JournalFileImpl: (hornetq-data-207.hq id = 227, recordID = 227)
#JournalFileImpl: (hornetq-data-208.hq id = 228, recordID = 228)

### Surviving Records Summary ###

### Prepared TX ###

### Message Counts ###
message count=0
message reference count
prepared message count=0


推荐答案

感谢Mr.Andy Taylor在上的回答。问题在于创建非持久性消息,例如:

Thanks for Mr.Andy Taylor at HornetQ User Forum for his answer. The problem was with creating not-persistent messages, like:

ClientMessage cMessage = session.createMessage(false);

我只需将其更改为 true ,并且它现在运作良好。

I just change it to true, and it's working well now.

这篇关于重启后HornetQ不会保留消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 09:49