hornetq 的使用实例如下,采用简单的嵌入式容器实现简单的HornetQ发送消息的简单实例。
关于hornetq的两个核心的配置文件为,hornetq-beans.xml和hornet-configuration.xml配置:
hornetq-beans.xml的配置如下:
<?xml version="1.0" encoding="UTF-8"?> <deployment xmlns="urn:jboss:bean-deployer:2.0"> <!--创建 MBean server --> <bean name="MBeanServer" class="javax.management.MBeanServer"> <constructor factoryClass="java.lang.management.ManagementFactory" factoryMethod="getPlatformMBeanServer"/> </bean> <!-- 创建一个核心文件配置 The core configuration --> <bean name="Configuration" class="org.hornetq.core.config.impl.FileConfiguration"/> <!-- 创建一个安全管理 The security manager --> <bean name="HornetQSecurityManager" class="org.hornetq.spi.core.security.HornetQSecurityManagerImpl"> <start ignored="true"/> <stop ignored="true"/> </bean> <!--创建一个核心服务器 The core server --> <bean name="HornetQServer" class="org.hornetq.core.server.impl.HornetQServerImpl"> <constructor> <parameter> <inject bean="Configuration"/> </parameter> <parameter> <inject bean="MBeanServer"/> </parameter> <parameter> <inject bean="HornetQSecurityManager"/> </parameter> </constructor> </bean> </deployment>
hornetq-configuration.xml配置如下:
<configuration xmlns="urn:hornetq" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:hornetq /schema/hornetq-configuration.xsd"> <!-- Acceptors --> <acceptors> <acceptor name="netty-acceptor"> <factory-class>org.hornetq.core.remoting.impl.netty.NettyAcceptorFactory</factory-class> <param key="tcp-no-delay" value="false"/> <param key="tcp-send-buffer-size" value="1048576"/> <param key="tcp-receive-buffer-size" value="1048576"/> </acceptor> </acceptors> <!-- 安全限制的设置 --> <security-enabled>false</security-enabled> <!-- 持久化操作的 --> <persistence-enabled>false</persistence-enabled> </configuration>
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); } }