2

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

这是代码:

// 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:

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
4

2 回答 2

1

感谢HornetQ 用户论坛的 Andy Taylor先生的回答。问题在于创建非持久消息,例如:

ClientMessage cMessage = session.createMessage(false);

我只是将其更改为true,现在它运行良好。

于 2013-01-15T09:22:19.687 回答
0

只要你在 hornetq-confuguration.xml 文件中让它持久化为真

于 2015-06-26T06:12:01.287 回答