zoukankan      html  css  js  c++  java
  • ActiveMQ系列之四:用ActiveMQ构建应用

    Broker:相当于一个ActiveMQ服务器实例 

      命令行启动参数示例如下: 

    1activemq start :使用默认的activemq.xml来启动 

    2activemq start xbean:file:../conf/activemq-2.xml :使用指定的配置文件来启动 

    3:如果不指定file,也就是xbean:activemq-2.xml,那么xml必须在classpath下面

    ActiveMQ来构建Java应用,这里主要将用ActiveMQ Broker作为独立的消息服务器来构建JAVA应用。ActiveMQ也支持在vm中通信基于嵌入式的broker,能够无缝的集成其它java应用 。       

      嵌入式Broker启动 

    1Broker Service启动broker ,示例如下: 

    BrokerService broker = new BrokerService();  
    
       broker.setUseJmx (true);  
    
       broker.addConnector ("tcp://localhost:61616");  
    
       broker.start (); 

    2BrokerFactory启动broker ,示例如下: 

       String Uri = "properties:broker.properties";  
    
       BrokerService broker1 = BrokerFactory.createBroker(new URI(Uri));  
    
       broker1.addConnector("tcp ://localhost:61616");  
    
       broker1.start(); 

    3broker.properties的内容示例如下:

       useJmx=true 
    
       persistent=false 
    
       brokerName=Cheese

    利用Spring集成BrokerSpring的配置文件如下: 

    <beans> 
    
        <bean id="admins" class="org.apache.activemq.security.AuthenticationUser"> 
    
               <constructor-arg index="0" value="admin" /> 
    
               <constructor-arg index="1" value="password" /> 
    
               <constructor-arg index="2" value="admins,publisher,consumers" /> 
    
        </bean> 
    
        <bean id="publishers"        class="org.apache.activemq.security.AuthenticationUser"> 
    
               <constructor-arg index="0" value="publisher" /> 
    
               <constructor-arg index="1" value="password" /> 
    
               <constructor-arg index="2" value="publisher,consumers" /> 
    
        </bean> 
    
        <bean id="consumers" class="org.apache.activemq.security.AuthenticationUser"> 
    
               <constructor-arg index="0" value="consumer" /> 
    
               <constructor-arg index="1" value="password" /> 
    
               <constructor-arg index="2" value="consumers" /> 
    
        </bean> 
    
        <bean id="guests" class="org.apache.activemq.security.AuthenticationUser"> 
    
               <constructor-arg index="0" value="guest" /> 
    
               <constructor-arg index="1" value="password" /> 
    
               <constructor-arg index="2" value="guests" /> 
    
        </bean> 
    <bean id="simpleAuthPlugin"         class="org.apache.activemq.security.SimpleAuthenticationPlugin"> 
    
                <property name="users"> 
    
          <util:list> 
    
    <ref bean="admins" /> 
    
    <ref bean="publishers" /> 
    
    <ref bean="consumers" /> 
    
    <ref bean="guests" /> 
    
          </util:list> 
    
                </property> 
    
         </bean> 
    
    <bean id="broker" class="org.apache.activemq.broker.BrokerService" init-method="start" destroy-method="stop"> 
    
                <property name="brokerName" value="myBroker" /> 
    
                <property name="persistent" value="false" /> 
    
                <property name="transportConnectorURIs"> 
    
          <list> 
    
    <value>tcp://localhost:61616</value> 
    
          </list> 
    
                </property> 
    
                <property name="plugins"> 
    
          <list> 
    
    <ref bean="simpleAuthPlugin"/> 
    
          </list> 
    
                </property> 
    
         </bean> 
    
    </beans> 

    或者配置BrokerFactoryBean,示例如下:

    <beans> 
    
       <bea id="broker"      class="org.apache.activemq.xbean.BrokerFactoryBean"> 
    
             <property name="config"  value="resources/activemq-simple.xml"/> 
    
             <property name="start" value="true" /> 
    
       </bean> 
    
    </beans> 

    1:可以通过在应用程序中以编码的方式启动broker,例如: broker.start();  

    如果需要启动多个broker,那么需要为broker设置一个名字。例如:

    BrokerService broker = new BrokerService();  
    
       broker.setName("fred");  
    
       broker.addConnector ("tcp://localhost:61616");  
    
       broker.start (); 
    

    2:还可以通过spring来启动,前面已经演示过了

     Spring提供了对JMS的支持,需要添加Spring支持jms的包,如下: 

    <dependency> 
    
       <groupId>org.springframework</groupId> 
    
       <artifactId>spring-jms</artifactId> 
    
       <version>4.0.3.RELEASE</version> 
    
    </dependency> 

     添加ActiveMQpool包 

    <dependency> 
    
       <groupId>org.apache.activemq</groupId> 
    
       <artifactId>activemq-pool</artifactId> 
    
       <version>5.9.0</version> 
    
    </dependency> 

     然后需要在Spring的配置文件中,配置jmsTemplate,示例如下: 

    <bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory" 
    
            destroy-method="stop">   
    
            <property name="connectionFactory">   
    
                <bea class="org.apache.activemq.ActiveMQConnectionFactory">   
    
                    <property name="brokerURL">   
    
      <value>tcp://192.168.1.106:61679</value>   
    
                    </property>   
    
                </bean>   
    
            </property>   
    
            <property name="maxConnections" value="100"></property>   
    
        </bean>   
    
        <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">   
    
            <property name="connectionFactory" ref="jmsFactory" />   
    
            <property name="defaultDestination" ref="destination" />   
    
            <property name="messageConverter">   
    
                <bea class="org.springframework.jms.support.converter.SimpleMessageConverter" />   
    
            </property>   
    
        </bean> 
        <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">   
    
            <constructor-arg index="0" value="spring-queue" />   
    
        </bean> 

    ActiveMQ结合Spring开发

     queue消息发送 
    
    @Autowired 
    
    private JmsTemplate jt = null; 
    
    public static void main(String[] args) { 
    
    ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); 
    
        SpringJMSClient ct = (SpringJMSClient)ctx.getBea ("springJMSClient"); 
    
        ct.jt.send(new MessageCreator() { 
    
               public Message createMessage(Sessio s) throws JMSExceptio { 
    
       TextMessage msg = s.createTextMessage("Spring msg==="); 
    
       retur msg; 
    
               } 
    
        }); 
    
    } 
    
     queue消息接收 
    
    @Autowired 
    
    private JmsTemplate jt = null; 
    
    public static void main(String[] args) throws Exceptio { 
    
    ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); 
    
    SpringJMSReceiverClient ct = (SpringJMSReceiverClient)ctx.getBean("springJMSReceiverClient"); 
    
        String msg =  (String)ct.jt.receiveAndConvert(); 
    
        System.out.println("msg==="+msg); 
    
    } 

    如果topic的话,首先需要修改spring的配置:先添加topic的配置,当然,需要修改jmsTemplate配置里面的defaultDestination,如果不想修改这个配置,那么直接把Destination注入程序,在程序里面选择发送的Destination也可以: 

    <bean id="destinationTopic" class="org.apache.activemq.command.ActiveMQTopic">   
    
        <constructor-arg index="0" value="spring-topic" />   
    
    </bean> 

    其他的客户端发送和接收跟队列基本是一样的。 如果想要在Spring中配置消费者的话,就不需要再启动接收的客户端了,配置如下: 

    <bean id="jmsContainer" 
    
       class="org.springframework.jms.listener.DefaultMessageListenerContainer"> 
    
       <property name="connectionFactory" ref="jmsFactory" /> 
    
       <property name="destination" ref="destinationTopic" /> 
    
       <property name="messageListener" ref="messageListener" /> 
    
    </bean> 
    
    <bean id="messageListener" 
    
       class="cn.javass.activemq.MyMessageListener"> 
    
    </bean> 

      当然,需要写一个类来实现消息监听,如:

    public class MyMessageListener implements MessageListener{ 
    
       public void onMessage(Message arg0) { 
    
             TextMessage msg =  (TextMessage)arg0; 
    
             try { 
    
                     System.out.println("receive txt msg==="+msg.getText ()); 
    
             } catch (JMSExceptio e) { 
    
                     e.printStackTrace(); 
    
             } 
    
       } 
    
    } 

    这样测试的时候,只需要启动消息生产者就好了。

     ActiveMQ结合Spring开发最佳实践和建议 

    1Camel框架支持大量的企业集成模式,可以大大简化集成组件间的大量服务和复杂的消息流。而Spring框架更注重简单性,仅仅支持基本的最佳实践。 

    2Spring消息发送的核心架构是JmsTemplate,隔离了像打开、关闭SessionProducer的繁琐操作,因此应用开发人员仅仅需要关注实际的业务逻辑。但是JmsTemplate损害了ActiveMQPooledConnectionFactorysession和消息producer的缓存机制而带来的性能提升。 

    3:新的Spring里面,可以设置org.springframework.jms.connection.CachingConnectionFactory的sessionCacheSize ,或者干脆使用ActiveMQPooledConnectionFactory 。

    4:不建议使用JmsTemplatereceive()调用,因为在JmsTemplate上的所有调用都是同步的,这意味着调用线程需要被阻塞,直到方法返回,这对性能影响很大 。

    5:请使用DefaultMessageListenerContainer,它允许异步接收消息并缓存session和消息consumer,而且还可以根据消息数量动态的增加或缩减监听器的数量。


    文档免费下载:ActiveMQ系列:ActiveMQ快速上手
    http://download.csdn.net/detail/undoner/8302247

  • 相关阅读:
    【排序】快速排序代码实现及优化
    【SpringMVC】重定向和转发
    RESTful风格
    【SpringMVC】用demo了解执行流程(xml配置)
    【Spring】声明式事务aop
    【Spring】整合Mybatis两种方式
    MongoDB语法与现有关系型数据库SQL语法比较
    Oracle查看哪些表被锁住了
    Mongodb分布式集群搭建
    四大MQ比较及MQ详解
  • 原文地址:https://www.cnblogs.com/wuyida/p/6300909.html
Copyright © 2011-2022 走看看