阅读更多

9顶
0踩

企业架构

原创新闻 一个使用Maven的HornetQ示例

2009-09-04 11:44 by 资深编辑 wutao0603 评论(2) 有9542人浏览
不久前,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
9
0
评论 共 2 条 请登录后发表评论
2 楼 whaosoft 2009-09-06 14:23
听起来不错!
1 楼 lakemove 2009-09-04 13:19
amazing !
Thanks !

发表评论

您还没有登录,请您登录后再发表评论

相关推荐

  • spring-boot-artemis-clustered-topic:一个示例项目,以集群模式使用主题(发布-订阅)演示通过Apache ActiveMQ Artemis 2.4.0在两个spring boot应用程序的生产者和使用者之间的异步通信。

    Spring Boot Artemis集群主题一个示例项目,通过集群模式下的topic (发布-订阅),通过Apache ActiveMQ Artemis 2.4.0演示了两个Spring Boot应用程序生产者和使用者之间的异步通信。介绍Apache ActiveMQ Artemis是...

  • 具有Eclipse和嵌入式JBoss HornetQ Server的简单JMS 1.1生产者和使用者示例

    在本文中,我们将使用Eclipse IDE和嵌入式JBoss HornetQ Server开发一个简单的JMS 1.1生产者和使用者示例。 发表简短目录 (Post Brief TOC) Introduction 介绍 Steps to Develop a JMS V.1.1 Example 开发JMS V.1.1...

  • spring和maven_具有Spring和Maven教程的JAX–WS

    spring和maven Spring框架通过JAX-WS提供对Web服务的远程支持,实际上,如Spring 参考文档中所述 ,有三种将Spring POJO服务公开为JAX-WS Web服务的方式: 公开基于Servlet的Web服务(适用于Java EE 5环境) ...

  • hornetq_Spring 3 HornetQ 2.1集成教程

    HornetQ是一个开放源代码项目,用于构建多协议,可嵌入,非常高性能的集群异步消息传递系统。 它是用Java编写的,并且可以在具有Java 5或更高版本运行时的任何平台上运行。 HornetQ一流的高性能日志以非持久消息...

  • 建立自己的GWT Spring Maven原型

    我们将展示的原型基于Justin的上一个项目,并包括各种技术,例如Spring , GWT , AspectJ , HornetQ和Infinispan 。 聊够了,现在让我们动手吧。 准备原型。 首先,您必须有一个用于构建原型的模板项目。在...

  • Spring 3 HornetQ 2.1集成教程

    HornetQ是一个开放源代码项目,用于构建多协议,可嵌入,非常高性能的集群异步消息传递系统。 它是用Java编写的,并且可以在具有Java 5或更高版本运行时的任何平台上运行。 HornetQ一流的高性能日志以非持久消息...

  • hornetq 集成 spring

    一、简介 HornetQ是一个支持集群和多种协议,可嵌入、高性能的异步消息系统。HornetQ完全支持JMS,HornetQ不但支持JMS1.1 API同时也定义属于自己的消息API,这可以最大限度的提升HornetQ的性能和灵活性。在不久的...

  • 带有Spring和Maven教程的JAX–WS

    Spring框架通过JAX-WS提供对Web服务的远程支持,实际上,如Spring 参考文档中所述 ,有三种将Spring ... 导出独立的Web服务(适用于使用Sun的JDK 1.6内置JAX–WS提供程序的情况) 使用JAX-WS RI的Spring支持导出We...

  • Spring Boot - 构建Spring Boot系统maven配置

     前面我们讲述了如何搭建一个简单的 Spring Boot 应用(参见Spring Boot - 初探),这里,我们来学习如何对项目进行相关的配置,包括系统构建、自动配置、依赖注入、开发工具等,使其更好地运行。 系统构建 ...

  • 使用Spring Boot CLI的Spring Boot Initilizr

    它使用默认设置在当前工作目录中创建一个新的Spring Boot Project zip文件,名为“ demo.zip”。 NOTE:- “spring init” Default settings: 注意:- “ spring init”默认设置: Default Build tool is “maven”....

  • GWT 2 Spring 3 JPA 2 Hibernate 3.5教程– Eclipse和Maven 2展示

    不久前,我的一个朋友和同事向我飞过,说“世界上只有一半在使用Maven ”。 当我意识到最受欢迎的文章(到现在为止) GWT 2 Spring 3 JPA 2 Hibernate 3.5 Tutorial提出了一种基于Google的Web Toolkit( GWT ) ...

  • 牛逼!Java 从入门到精通,超全汇总版

    设计模式并不适合一口气读完,因为你看完几个设计模式就会容易混,相信我,你可以一周熟悉一个设计模式,这样在工作中你也可以使用。一口气看完所有,就会记住最后一个设计模式,相信我,亲身实践。。。。。。 Java...

  • HowToDoInJava 其它教程 2 · 翻译完毕

    原文:HowToDoInJava 协议:CC BY-NC-SA 4.0 欢迎任何人参与和完善:一个人可以走的很快,但是一群人却可以走的更远。...使用 Maven 的 HornetQ 独立服务器示例 Spring3 Hornetq 独立集成示例 Gson 教程 Gson .

  • springboot使用教程

    Springboot使用其他json转换框架4. Springboot全局异常捕捉5. Springboot JPA 连接数据库6. Springboot 配置JPA7. Springboot整合JPA保存数据8. Springboot使用 jdbcTemplate保存数据9. springboot 常用配置10. ...

  • gwt-2.8.2下载_GWT 2 Spring 3 JPA 2 Hibernate 3.5教程– Eclipse和Maven 2展示

    不久前,我的一个朋友和同事向我飞过,说“世界上只有一半在使用Maven ”。 当我意识到最受欢迎的文章(到目前为止) GWT 2 Spring 3 JPA 2 Hibernate 3.5 Tutorial提出了一种基于Google的Web Toolkit( GWT ) ...

  • JMS + jboss EAP 6.2 示例

    .Net中如果需要消息队列功能,可以很方便的使用...一种是点对点模式(即“一条”消息只能从“一个”发送方传输到“一个”接收方) - 这种模式下,发送方、接收方不必同时在线,消息在未取回走,一直存放在队列中。...

  • III. 使用Spring Boot

    尽管Spring Boot没有什么特别的(只是一个你能消费的库),但仍有一些建议,如果你遵循的话将会让你的开发进程更容易。如果你刚接触Spring Boot,那最好先读下上一章节的Getting Started指南。构建系统强烈

  • SpringSide实战(一)----SpringSide简介

    最近在公司用JUP框架做项目,发现这个框架是别人基于SpringSide封装的,所以打算学习下,SpringSide,其中遇到了很多坑,做个记录,网上关于这方面的资料都有些老了,而且SpringSide最新的版本是SpringSide-Utils,老一点的...

  • 中国石油大学(北京)克拉玛依校区在广东2021-2024各专业最低录取分数及位次表.pdf

    全国各大学在广东省2021~2024年各专业最低录取分数及位次

  • 浙江越秀外国语学院在广东2021-2024各专业最低录取分数及位次表.pdf

    全国各大学在广东省2021~2024年各专业最低录取分数及位次

Global site tag (gtag.js) - Google Analytics