簡體   English   中英

如何在HornetQ 2.2.5核心客戶端中保留隊列?

[英]How to make a queue persisted in HornetQ 2.2.5 core client?

我想在核心hornetQ客戶端中建立持久隊列。 問題是當我停止服務器隊列時,數據將被銷毀。 如何保持隊列持久化? 我的代碼是:

import java.util.Date;
import org.hornetq.api.core.TransportConfiguration;
import org.hornetq.api.core.client.ClientConsumer;
import org.hornetq.api.core.client.ClientMessage;
import org.hornetq.api.core.client.ClientProducer;
import org.hornetq.api.core.client.ClientSession;
import org.hornetq.api.core.client.ClientSessionFactory;
import org.hornetq.api.core.client.HornetQClient;
import org.hornetq.api.core.client.ServerLocator;
import org.hornetq.core.config.Configuration;
import org.hornetq.core.config.impl.ConfigurationImpl;
import org.hornetq.core.remoting.impl.invm.InVMAcceptorFactory;
import org.hornetq.core.remoting.impl.invm.InVMConnectorFactory;
import org.hornetq.core.server.HornetQServer;
import org.hornetq.core.server.HornetQServers;

public class EmbeddedExample
{

   public static void main(final String[] args)
   {
      try
      {

         // Step 1. Create the Configuration, and set the properties accordingly
         Configuration configuration = new ConfigurationImpl();
         configuration.setPersistenceEnabled(false);
         configuration.setSecurityEnabled(false);

         configuration.getAcceptorConfigurations().add(new TransportConfiguration(InVMAcceptorFactory.class.getName()));

         // Step 2. Create and start the server
         HornetQServer server = HornetQServers.newHornetQServer(configuration);
         server.start();

         // Step 3. As we are not using a JNDI environment we instantiate the objects directly
         ServerLocator serverLocator = HornetQClient.createServerLocatorWithoutHA(new TransportConfiguration(InVMConnectorFactory.class.getName()));
         ClientSessionFactory sf = serverLocator.createSessionFactory();        

         // Step 4. Create a core queue        
         ClientSession coreSession = sf.createSession(false, false, false);      

         final String queueName = "queue.exampleQueue";

         coreSession.createQueue(queueName, queueName, true);


         coreSession.close();

         ClientSession session = null;

         try
         {

            // Step 5. Create the session, and producer
            session = sf.createSession();

           ClientProducer producer = session.createProducer(queueName);

            // Step 6. Create and send a message
            ClientMessage message = session.createMessage(true);

            final String propName = "myprop";

            message.putStringProperty(propName, "Hello sent at " + new Date());

            System.out.println("Producer:");
            System.out.println("StartDate: "+new Date());
             for (int i = 0; i < 100000; i++)
             {  
                   message = session.createMessage(true); // move it   
                    message.putStringProperty(propName, "Message: " + i);
                    producer.send(message);       
             }
            System.out.println("EndDate: "+new Date());
            // Step 7. Create the message consumer and start the connection
            ClientConsumer messageConsumer = session.createConsumer(queueName);

            session.start();

            // Step 8. Receive the message.
            System.out.println("Consumer:");
            System.out.println("StartDate: "+new Date());

            //for (int i = 0; i <= 100000; i++)         
             int i=0;
            while(true)
            {   
                 i++;
                 if(i == 10000){    
                     i=0;
                     session.start();
                     System.out.println("EndDate: "+new Date());                        
                 }
                 ClientMessage messageReceived = messageConsumer.receive(5000);
                 if (messageReceived!=null) messageReceived.acknowledge();
                 //System.out.println(messageReceived.getStringProperty(propName));
            }

         }
         finally
         {
            // Step 9. Be sure to close our resources!
            if (sf != null)
            {
               sf.close();
            }

            // Step 10. Stop the server
            server.stop();
         }
      }
      catch (Exception e)
      {
         e.printStackTrace();
         System.exit(-1);
      }
   }
}

如果禁用持久性,則不會持久化:

Configuration configuration = new ConfigurationImpl();
configuration.setPersistenceEnabled(true); <<<<  Make this true

UnsatisfiedLinkError可能是因為您選擇了AIO並且LD_LIBRARY_PATH上沒有本機庫,要么設置Journal,要么在Linux系統中使本機庫可用。

configuration.setJournalType(JournalType.NIO);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM