JBOSS 消息中间件hornetq的使用(一)

             hornetq 的使用实例如下,采用简单的嵌入式容器实现简单的HornetQ发送消息的简单实例。

关于hornetq的两个核心的配置文件为,hornetq-beans.xml和hornet-configuration.xml配置:

hornetq-beans.xml的配置如下:




    
   
      
   

   
   

   
   
      
      
   

   
   
      
         
            
         
         
            
         
         
            
         
      
   


 

hornetq-configuration.xml配置如下:



   
   
   
      
         org.hornetq.core.remoting.impl.netty.NettyAcceptorFactory    
         
         
         
      
   
    
   false
   
   false
 

 

 

hornetq的嵌入式代码实现如下:

package com.easyway.hornetq.server;
import java.util.Date;
import org.hornetq.api.core.TransportConfiguration;
import org.hornetq.api.core.client.*;
import org.hornetq.core.remoting.impl.netty.NettyConnectorFactory;
import org.hornetq.integration.bootstrap.HornetQBootstrapServer;
/**
 *
 *创建嵌入式的容器发送和创建消息
 * 
 * @author longgangbai
 *
 */
public class EmbeddedMicroContainer
{

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

      HornetQBootstrapServer hornetQ = null;
      try
      {
    	 //1.启动相关的服务
    	// Step 1. Start the server
         hornetQ = new HornetQBootstrapServer("./hornetq-beans.xml");
         hornetQ.run();

         //2.使用对象创建相关的客户端工厂 ,不采用JNDI环境创建创建相关的实例
         // Step 2. As we are not using a JNDI environment we instantiate the objects directly
         ClientSessionFactory sf = HornetQClient.createClientSessionFactory(new TransportConfiguration(NettyConnectorFactory.class.getName()));

         //3.创建一个核心会话信息并创建相关的队列
         //Step 3. Create a core queue
         ClientSession coreSession = sf.createSession(false, false, false);

         final String queueName = "queue/longgangbai";

         coreSession.createQueue(queueName, queueName, true);

         coreSession.close();

         ClientSession session = null;

         try
         {
            // Step 4. Create the session, and producer
        	//创建一个会话和创建者
            session = sf.createSession();
            ClientProducer producer = session.createProducer(queueName);

            // Step 5. Create and send a message
            //6.创建消息并发送消息
            ClientMessage message = session.createMessage(false);

            final String propName = "myprop";
            message.putStringProperty(propName, "Hello sent at " + new Date());

            System.out.println("Sending the message.");
            //发送相关的消息
            producer.send(message);

            // Step 6. Create the message consumer and start the connection
            //6.创建消息的消费者并连接相关的连接
            ClientConsumer messageConsumer = session.createConsumer(queueName);
            session.start();

            // Step 7. Receive the message.
            ClientMessage messageReceived = messageConsumer.receive(1000);
            //获取相关的字符串信息
            System.out.println("Received TextMessage:" + messageReceived.getStringProperty(propName));
         }
         finally
         {
            // Step 8. Be sure to close our resources!
        	 //8.关闭相关的队列
            if (session != null)
            {
               session.close();
            }

            // Step 9. Shutdown the container
            //9.关闭相关的容器
            if (hornetQ != null)
            {
               hornetQ.shutDown();
            }
         }
      }
      catch (Exception e)
      {
         e.printStackTrace();
         System.exit(-1);
      }
   }

 

你可能感兴趣的:(JBOSS 消息中间件hornetq的使用(一))