一个使用Maven的HornetQ示例
程序员文章站
2022-05-11 21:59:04
...
不久前,JBoss middleware Messaging开发团队将Jboss Messaging 2.0取名为 HornetQ,一个面向消息的中间件。
自从HornetQ可以被嵌入并能在几乎没有依赖或没有第三方依赖的情况下运行之后,创建一个运行嵌入式服务器或简单客户端的项目变的相当容易。
以下代码可以在此下载:http://www.jboss.org/community/servlet/JiveServlet/downloadBody/14103-102-1-106602/HornetQMavenExample.zip
嵌入式服务器
在嵌入式服务器目录下寻找pom.xml中的依赖。需要如何配置依赖:
1. <dependencies>
2. <dependency>
3. <groupId>org.hornetq</groupId>
4. <artifactId>hornetq-core</artifactId>
5. <version>2.0.0.BETA5</version>
6. <scope>compile</scope>
7. </dependency>
8. <dependency>
9. <groupId>org.hornetq</groupId>
10. <artifactId>hornetq-jms</artifactId>
11. <version>2.0.0.BETA5</version>
12. <scope>compile</scope>
13. </dependency>
14. <dependency>
15. <groupId>org.hornetq</groupId>
16. <artifactId>hornetq-logging</artifactId>
17. <version>2.0.0.BETA5</version>
18. <scope>compile</scope>
19. </dependency>
20. <dependency>
21. <groupId>org.hornetq</groupId>
22. <artifactId>hornetq-transports</artifactId>
23. <version>2.0.0.BETA5</version>
24. <scope>compile</scope>
25. </dependency>
26. <dependency>
27. <groupId>org.jboss.netty</groupId>
28. <artifactId>netty</artifactId>
29. <version>3.1.0.GA</version>
30. </dependency>
31. <dependency>
32. <groupId>org.jboss.javaee</groupId>
33. <artifactId>jboss-jms-api</artifactId>
34. <version>1.1.0.GA</version>
35. <scope>compile</scope>
36. </dependency>
以上这些依赖是需要在HornetQ JMS server运行,再来看一下需要运行在嵌入式服务的代码:
1. public class EmbeddedServer
2. {
3. public static void main(String[] args) throws Exception
4. {
5. try
6. {
7. FileConfiguration configuration = new FileConfiguration();
8. configuration.setConfigurationUrl("hornetq-configuration.xml");
9. configuration.start();
10.
11. HornetQServer server = HornetQ.newHornetQServer(configuration);
12. JMSServerManager jmsServerManager = new JMSServerManagerImpl(server, "hornetq-jms.xml");
13. //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.
14. jmsServerManager.setContext(null);
15. jmsServerManager.start();
16. System.out.println("STARTED::");
17. }
18. catch (Throwable e)
19. {
20. System.out.println("FAILED::");
21. e.printStackTrace();
22. }
23. }
24. }
你可以通过hornetq-configuration.xml文件来配置服务,hornetq-jms.xml文件来配置任何JMS对象。
客户端
客户端的例子说明了如何轻松的使用MInimal jar 创建一个HornetQ JMS client。
# <dependency>
# <groupId>org.hornetq</groupId>
# <artifactId>hornetq-core-client</artifactId>
# <version>2.0.0.BETA5</version>
# <scope>compile</scope>
# </dependency>
# <dependency>
# <groupId>org.hornetq</groupId>
# <artifactId>hornetq-jms-client</artifactId>
# <version>2.0.0.BETA5</version>
# <scope>compile</scope>
# </dependency>
# <dependency>
# <groupId>org.hornetq</groupId>
# <artifactId>hornetq-transports</artifactId>
# <version>2.0.0.BETA5</version>
# <scope>compile</scope>
# </dependency>
# <dependency>
# <groupId>org.jboss.netty</groupId>
# <artifactId>netty</artifactId>
# <version>3.1.0.GA</version>
# </dependency>
# <dependency>
# <groupId>org.jboss.javaee</groupId>
# <artifactId>jboss-jms-api</artifactId>
# <version>1.1.0.GA</version>
# <scope>compile</scope>
# </dependency>
客户端代码就爱那个会负责一个HornetQ示例,在启动运行之前,这个客户端将会像服务器发送一个信息。
1. public static void main(String[] args) throws Exception
2. {
3. Connection connection = null;
4. try
5. {
6. // Step 1. Directly instantiate the JMS Queue object.
7. Queue queue = new HornetQQueue("exampleQueue");
8.
9. // Step 2. Instantiate the TransportConfiguration object which contains the knowledge of what transport to use,
10. // The server port etc.
11.
12. Map<string, object=""> connectionParams = new HashMap<string, object="">();
13. connectionParams.put(PORT_PROP_NAME, 5445);
14.
15. TransportConfiguration transportConfiguration = new TransportConfiguration(NettyConnectorFactory.class.getName(),
16. connectionParams);
17.
18. // Step 3 Directly instantiate the JMS ConnectionFactory object using that TransportConfiguration
19. ConnectionFactory cf = new HornetQConnectionFactory(transportConfiguration);
20.
21. // Step 4.Create a JMS Connection
22. connection = cf.createConnection();
23.
24. // Step 5. Create a JMS Session
25. Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
26.
27. // Step 6. Create a JMS Message Producer
28. MessageProducer producer = session.createProducer(queue);
29.
30. // Step 7. Create a Text Message
31. TextMessage message = session.createTextMessage("This is a text message");
32.
33. System.out.println("Sent message: " + message.getText());
34.
35. // Step 8. Send the Message
36. producer.send(message);
37.
38. // Step 9. Create a JMS Message Consumer
39. MessageConsumer messageConsumer = session.createConsumer(queue);
40.
41. // Step 10. Start the Connection
42. connection.start();
43.
44. // Step 11. Receive the message
45. TextMessage messageReceived = (TextMessage)messageConsumer.receive(5000);
46.
47. System.out.println("Received message: " + messageReceived.getText());
48. }
49. finally
50. {
51. if (connection != null)
52. {
53. connection.close();
54. }
55. }
56. }
57. </string,></string,>
你可以使用上述代码作为入门示例,将HornerQ作为一个客户端或嵌入式服务器。
自从HornetQ可以被嵌入并能在几乎没有依赖或没有第三方依赖的情况下运行之后,创建一个运行嵌入式服务器或简单客户端的项目变的相当容易。
以下代码可以在此下载:http://www.jboss.org/community/servlet/JiveServlet/downloadBody/14103-102-1-106602/HornetQMavenExample.zip
嵌入式服务器
在嵌入式服务器目录下寻找pom.xml中的依赖。需要如何配置依赖:
1. <dependencies>
2. <dependency>
3. <groupId>org.hornetq</groupId>
4. <artifactId>hornetq-core</artifactId>
5. <version>2.0.0.BETA5</version>
6. <scope>compile</scope>
7. </dependency>
8. <dependency>
9. <groupId>org.hornetq</groupId>
10. <artifactId>hornetq-jms</artifactId>
11. <version>2.0.0.BETA5</version>
12. <scope>compile</scope>
13. </dependency>
14. <dependency>
15. <groupId>org.hornetq</groupId>
16. <artifactId>hornetq-logging</artifactId>
17. <version>2.0.0.BETA5</version>
18. <scope>compile</scope>
19. </dependency>
20. <dependency>
21. <groupId>org.hornetq</groupId>
22. <artifactId>hornetq-transports</artifactId>
23. <version>2.0.0.BETA5</version>
24. <scope>compile</scope>
25. </dependency>
26. <dependency>
27. <groupId>org.jboss.netty</groupId>
28. <artifactId>netty</artifactId>
29. <version>3.1.0.GA</version>
30. </dependency>
31. <dependency>
32. <groupId>org.jboss.javaee</groupId>
33. <artifactId>jboss-jms-api</artifactId>
34. <version>1.1.0.GA</version>
35. <scope>compile</scope>
36. </dependency>
以上这些依赖是需要在HornetQ JMS server运行,再来看一下需要运行在嵌入式服务的代码:
1. public class EmbeddedServer
2. {
3. public static void main(String[] args) throws Exception
4. {
5. try
6. {
7. FileConfiguration configuration = new FileConfiguration();
8. configuration.setConfigurationUrl("hornetq-configuration.xml");
9. configuration.start();
10.
11. HornetQServer server = HornetQ.newHornetQServer(configuration);
12. JMSServerManager jmsServerManager = new JMSServerManagerImpl(server, "hornetq-jms.xml");
13. //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.
14. jmsServerManager.setContext(null);
15. jmsServerManager.start();
16. System.out.println("STARTED::");
17. }
18. catch (Throwable e)
19. {
20. System.out.println("FAILED::");
21. e.printStackTrace();
22. }
23. }
24. }
你可以通过hornetq-configuration.xml文件来配置服务,hornetq-jms.xml文件来配置任何JMS对象。
客户端
客户端的例子说明了如何轻松的使用MInimal jar 创建一个HornetQ JMS client。
# <dependency>
# <groupId>org.hornetq</groupId>
# <artifactId>hornetq-core-client</artifactId>
# <version>2.0.0.BETA5</version>
# <scope>compile</scope>
# </dependency>
# <dependency>
# <groupId>org.hornetq</groupId>
# <artifactId>hornetq-jms-client</artifactId>
# <version>2.0.0.BETA5</version>
# <scope>compile</scope>
# </dependency>
# <dependency>
# <groupId>org.hornetq</groupId>
# <artifactId>hornetq-transports</artifactId>
# <version>2.0.0.BETA5</version>
# <scope>compile</scope>
# </dependency>
# <dependency>
# <groupId>org.jboss.netty</groupId>
# <artifactId>netty</artifactId>
# <version>3.1.0.GA</version>
# </dependency>
# <dependency>
# <groupId>org.jboss.javaee</groupId>
# <artifactId>jboss-jms-api</artifactId>
# <version>1.1.0.GA</version>
# <scope>compile</scope>
# </dependency>
客户端代码就爱那个会负责一个HornetQ示例,在启动运行之前,这个客户端将会像服务器发送一个信息。
1. public static void main(String[] args) throws Exception
2. {
3. Connection connection = null;
4. try
5. {
6. // Step 1. Directly instantiate the JMS Queue object.
7. Queue queue = new HornetQQueue("exampleQueue");
8.
9. // Step 2. Instantiate the TransportConfiguration object which contains the knowledge of what transport to use,
10. // The server port etc.
11.
12. Map<string, object=""> connectionParams = new HashMap<string, object="">();
13. connectionParams.put(PORT_PROP_NAME, 5445);
14.
15. TransportConfiguration transportConfiguration = new TransportConfiguration(NettyConnectorFactory.class.getName(),
16. connectionParams);
17.
18. // Step 3 Directly instantiate the JMS ConnectionFactory object using that TransportConfiguration
19. ConnectionFactory cf = new HornetQConnectionFactory(transportConfiguration);
20.
21. // Step 4.Create a JMS Connection
22. connection = cf.createConnection();
23.
24. // Step 5. Create a JMS Session
25. Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
26.
27. // Step 6. Create a JMS Message Producer
28. MessageProducer producer = session.createProducer(queue);
29.
30. // Step 7. Create a Text Message
31. TextMessage message = session.createTextMessage("This is a text message");
32.
33. System.out.println("Sent message: " + message.getText());
34.
35. // Step 8. Send the Message
36. producer.send(message);
37.
38. // Step 9. Create a JMS Message Consumer
39. MessageConsumer messageConsumer = session.createConsumer(queue);
40.
41. // Step 10. Start the Connection
42. connection.start();
43.
44. // Step 11. Receive the message
45. TextMessage messageReceived = (TextMessage)messageConsumer.receive(5000);
46.
47. System.out.println("Received message: " + messageReceived.getText());
48. }
49. finally
50. {
51. if (connection != null)
52. {
53. connection.close();
54. }
55. }
56. }
57. </string,></string,>
你可以使用上述代码作为入门示例,将HornerQ作为一个客户端或嵌入式服务器。