原文: https://howtodoinjava.com/hornetq/basic-jms-messaging-example-using-hornetq-stand-alone-server/

    HornetQ 是一个开放源代码项目,旨在构建多协议,可嵌入,非常高性能的集群异步消息传递系统。 HornetQ 支持 JMS 1.1 API,并且还定义了自己的消息传递 API,以实现最佳性能和灵活性。 HornetQ 一流的高性能日志以非持久消息传递通常看到的速度提供持久消息传递性能。 HornetQ 提供服务器复制和自动客户端故障转移功能,以消除服务器故障时丢失或重复的消息。

    在上一篇文章中,我们了解了有关配置独立 hornetq 服务器和基本配置的信息。 让我们继续举例。

    在本文中,我们将学习将 JMS 消息发送到 hornetq 服务器上的队列的机制,然后检索这些消息。

    步骤 1)使用以下命令创建一个 maven 项目并将其转换为 Eclipse Java 项目

    1. mvn archetype:generate -DgroupId=com.howtodoinjava -DartifactId=HornetQHelloWorld
    2. -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
    3. cd HornetQHelloWorld
    4. mvn eclipse:eclipse

    步骤 2)更新pom.xml文件并更新项目依赖项

    pom.xml

    1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    3. <modelVersion>4.0.0</modelVersion>
    4. <groupId>com.howtodoinjava</groupId>
    5. <artifactId>HornetQHelloWorld</artifactId>
    6. <packaging>jar</packaging>
    7. <version>1.0-SNAPSHOT</version>
    8. <name>HornetQHelloWorld</name>
    9. <url>http://maven.apache.org</url>
    10. <dependencies>
    11. <dependency>
    12. <groupId>junit</groupId>
    13. <artifactId>junit</artifactId>
    14. <version>3.8.1</version>
    15. <scope>test</scope>
    16. </dependency>
    17. <dependency>
    18. <groupId>org.hornetq</groupId>
    19. <artifactId>hornetq-core</artifactId>
    20. <version>2.0.0.GA</version>
    21. <scope>compile</scope>
    22. </dependency>
    23. <dependency>
    24. <groupId>org.hornetq</groupId>
    25. <artifactId>hornetq-jms</artifactId>
    26. <version>2.0.0.GA</version>
    27. <scope>compile</scope>
    28. </dependency>
    29. <dependency>
    30. <groupId>org.hornetq</groupId>
    31. <artifactId>hornetq-logging</artifactId>
    32. <version>2.0.0.GA</version>
    33. <scope>compile</scope>
    34. </dependency>
    35. <dependency>
    36. <groupId>org.hornetq</groupId>
    37. <artifactId>hornetq-transports</artifactId>
    38. <version>2.0.0.GA</version>
    39. <scope>compile</scope>
    40. </dependency>
    41. <dependency>
    42. <groupId>org.jboss.netty</groupId>
    43. <artifactId>netty</artifactId>
    44. <version>3.1.0.GA</version>
    45. </dependency>
    46. <dependency>
    47. <groupId>org.jboss.javaee</groupId>
    48. <artifactId>jboss-jms-api</artifactId>
    49. <version>1.1.0.GA</version>
    50. <scope>compile</scope>
    51. </dependency>
    52. </dependencies>
    53. </project>

    步骤 3)将基本的 hornetq 配置文件放在类路径中。

    hornetq-configuration.xml

    1. <?xml version="1.0"?>
    2. <configuration xsi:schemaLocation="urn:hornetq /schema/hornetq-configuration.xsd"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:hornetq">
    4. <connectors>
    5. <connector name="netty-connector">
    6. <factory-class>org.hornetq.integration.transports.netty.NettyConnectorFactory
    7. </factory-class>
    8. </connector>
    9. </connectors>
    10. <acceptors>
    11. <acceptor name="netty-acceptor">
    12. <factory-class>org.hornetq.integration.transports.netty.NettyAcceptorFactory
    13. </factory-class>
    14. </acceptor>
    15. </acceptors>
    16. <security-enabled>false</security-enabled>
    17. </configuration>

    步骤 4)配置连接器工厂,并将配置文件放置在classpath中。

    hornetq-jms.xml

    1. <?xml version="1.0"?>
    2. <configuration xsi:schemaLocation="urn:hornetq /schema/hornetq-jms.xsd"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:hornetq">
    4. <!--the connection factory used by the example -->
    5. <connection-factory name="ConnectionFactory">
    6. <connectors>
    7. <connector-ref connector-name="netty-connector" />
    8. </connectors>
    9. <entries>
    10. <entry name="ConnectionFactory" />
    11. </entries>
    12. </connection-factory>
    13. <queue name="exampleQueue">
    14. <entry name="exampleQueue" />
    15. </queue>
    16. </configuration>

    步骤 5)启动服务器并测试消息传递代码

    HornetQMessageQueueDemo.java

    1. package com.howtodoinjava;
    2. import java.util.HashMap;
    3. import java.util.Map;
    4. import javax.jms.Connection;
    5. import javax.jms.ConnectionFactory;
    6. import javax.jms.MessageConsumer;
    7. import javax.jms.MessageProducer;
    8. import javax.jms.Queue;
    9. import javax.jms.Session;
    10. import javax.jms.TextMessage;
    11. import org.hornetq.api.core.TransportConfiguration;
    12. import org.hornetq.api.jms.HornetQJMSClient;
    13. import org.hornetq.core.config.impl.FileConfiguration;
    14. import org.hornetq.core.server.HornetQServer;
    15. import org.hornetq.core.server.HornetQServers;
    16. import org.hornetq.integration.transports.netty.NettyConnectorFactory;
    17. import org.hornetq.integration.transports.netty.TransportConstants;
    18. import org.hornetq.jms.server.JMSServerManager;
    19. import org.hornetq.jms.server.impl.JMSServerManagerImpl;
    20. public class HornetQMessageQueueDemo {
    21. static void startServer()
    22. {
    23. try
    24. {
    25. FileConfiguration configuration = new FileConfiguration();
    26. configuration.setConfigurationUrl("hornetq-configuration.xml");
    27. configuration.start();
    28. HornetQServer server = HornetQServers.newHornetQServer(configuration);
    29. JMSServerManager jmsServerManager = new JMSServerManagerImpl(server, "hornetq-jms.xml");
    30. //if you want to use JNDI, simple inject a context here or don't call this method and make sure the JNDI parameters are set.
    31. jmsServerManager.setContext(null);
    32. jmsServerManager.start();
    33. System.out.println("Server started !!");
    34. }
    35. catch (Throwable e)
    36. {
    37. System.out.println("Damn it !!");
    38. e.printStackTrace();
    39. }
    40. }
    41. public static void main(String[] args) throws Exception
    42. {
    43. //Start the server
    44. startServer();
    45. Connection connection = null;
    46. try
    47. {
    48. // Step 1\. Directly instantiate the JMS Queue object.
    49. Queue queue = HornetQJMSClient.createQueue("exampleQueue");
    50. // Step 2\. Instantiate the TransportConfiguration object which
    51. // contains the knowledge of what transport to use,
    52. // The server port etc.
    53. Map<String, Object> connectionParams = new HashMap<String, Object>();
    54. connectionParams.put(TransportConstants.PORT_PROP_NAME, 5445);
    55. TransportConfiguration transportConfiguration = new TransportConfiguration(
    56. NettyConnectorFactory.class.getName(), connectionParams);
    57. // Step 3 Directly instantiate the JMS ConnectionFactory object
    58. // using that TransportConfiguration
    59. ConnectionFactory cf = HornetQJMSClient.createConnectionFactory(transportConfiguration);
    60. // Step 4.Create a JMS Connection
    61. connection = cf.createConnection();
    62. // Step 5\. Create a JMS Session
    63. Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
    64. // Step 6\. Create a JMS Message Producer
    65. MessageProducer producer = session.createProducer(queue);
    66. // Step 7\. Create a Text Message
    67. TextMessage message = session.createTextMessage("How to do in java dot com");
    68. System.out.println("Sent message: " + message.getText());
    69. // Step 8\. Send the Message
    70. producer.send(message);
    71. // Step 9\. Create a JMS Message Consumer
    72. MessageConsumer messageConsumer = session.createConsumer(queue);
    73. // Step 10\. Start the Connection
    74. connection.start();
    75. // Step 11\. Receive the message
    76. TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000);
    77. System.out.println("Received message: " + messageReceived.getText());
    78. }
    79. finally
    80. {
    81. if (connection != null) {
    82. connection.close();
    83. }
    84. }
    85. }
    86. }
    87. Output in console:
    88. 22 Mar, 2013 2:23:36 PM org.hornetq.core.logging.impl.JULLogDelegate info
    89. INFO: live server is starting..
    90. 22 Mar, 2013 2:23:36 PM org.hornetq.core.logging.impl.JULLogDelegate warn
    91. WARNING: AIO wasn't located on this platform, it will fall back to using pure Java NIO. If your platform is Linux, install LibAIO to enable the AIO journal
    92. 22 Mar, 2013 2:23:36 PM org.hornetq.core.logging.impl.JULLogDelegate info
    93. INFO: Using NIO Journal
    94. 22 Mar, 2013 2:23:36 PM org.hornetq.core.logging.impl.JULLogDelegate warn
    95. WARNING: Security risk! It has been detected that the cluster admin user and password have not been changed from the installation default. Please see the HornetQ user guide, cluster chapter, for instructions on how to do this.
    96. 22 Mar, 2013 2:23:36 PM org.hornetq.core.logging.impl.JULLogDelegate info
    97. INFO: Started Netty Acceptor version 3.1.5.GA-r1772
    98. Server started !!
    99. 22 Mar, 2013 2:23:36 PM org.hornetq.core.logging.impl.JULLogDelegate info
    100. INFO: HornetQ Server version 2.0.0.GA (Hornet Queen, 113) started
    101. Sent message: How to do in java dot com
    102. Received message: How to do in java dot com

    下载源代码

    祝您学习愉快!