ActiveMQ (): ActiveMQ combined with spring development--the second way

Source: Internet
Author: User

First, Pom.xml and mq.properties

Spring provides support for JMS and needs to add spring-supported JMS packages, as follows:

<dependency> <groupId>org.springframework</groupId> <artifactid>spring-jms</ Artifactid> <version>4.1.7.RELEASE</version></dependency>

Add the ACTIVEMQ pool package as follows:

<dependency> <groupId>org.apache.activemq</groupId> <artifactid>activemq-pool</ Artifactid> <version>5.11.1</version></dependency>

Add the Xbean label configuration as follows:

<dependency> <groupId>org.apache.xbean</groupId> <artifactid>xbean-spring</artifactid > <version>3.16</version></dependency>

Mq.properties:

activemq.brokerurl=tcp://192.168.91.8:61616activemq.username=liuyactivemq.password= 123456activemq.maxconnections=100activemq.destination.name=spring-queueactivemq.destinationtopic.name= Spring-topic


Second, mq.xml Configuration

<?xml version= "1.0"  encoding= "UTF-8"? ><beans xmlns= "http://www.springframework.org/ Schema/beans "       xmlns:context=" http://www.springframework.org/schema/ Context "        xmlns:p=" http://www.springframework.org/schema/p "        xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"          xmlns:tx= "Http://www.springframework.org/schema/tx"         xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"         xsi:schemalocation= "           http:// Www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd            http://www.springframework.org/schema/ Context http://www.springframework.org/schema/context/spring-context-4.0.xsd           http:// www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd             http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-4.0.xsd            http://www.springframework.org/schema/util http://www.springframework.org/schema/ Util/spring-util-4.0.xsd "><bean id=" Jmsfactory " class=" Org.apache.activemq.pool.PooledConnectionFactory " destroy-method=" Stop ">             <property name= "ConnectionFactory" >         <bean class= "Org.apache.activemq.ActiveMQConnectionFactory" >     <property name= "Brokerurl" &GT;&NBSP;&NBSP;&Nbsp;     <value>${activemq.brokerurl}</value>    < /property>    <property name= "UserName"  value= "${activemq.username}" > </property>                 <property name= "Password"  value= "${activemq.password}" ></property> </bean >    </property>    <property name= "MaxConnections"  value= "${activemq.maxconnections}" ></property></bean><bean id= "JmsTemplate"   class= "Org.springframework.jms.core.JmsTemplate" >    <property name= " ConnectionFactory " ref=" jmsfactory " />    <!--  Set the default destination  -->     <property name= "Defaultdestination"  ref= "DestinationTopic"  />     <properTy name= "Messageconverter" >        <bean class= " Org.springframework.jms.support.converter.SimpleMessageConverter " />    </ property></bean><!--  Destination: queue --><bean id= "Destination"  class= " Org.apache.activemq.command.ActiveMQQueue ">    <constructor-arg name=" name "  value= "${activemq.destination.name}"  /></bean><!--  Destination: Topic, non-persistent  -->< Bean id= "Destinationtopic"  class= "Org.apache.activemq.command.ActiveMQTopic" >     <constructor-arg name= "name"  value= "${activemq.destinationtopic.name}"  /></bean ></beans>


Third, message sending and receiving

package com.liuy.spring;import javax.jms.jmsexception;import javax.jms.message;import  javax.jms.session;import javax.jms.textmessage;import  org.springframework.beans.factory.annotation.autowired;import org.springframework.jms.core.jmstemplate; import org.springframework.jms.core.messagecreator;import org.springframework.stereotype.service;/**  *  @description   Description  *  @author  liuyu *  @version  1.0 * @ date:2017 April 11 afternoon 8:32:46 */@Servicepublic  class mqservice {    @ autowired    private jmstemplate jt;         /** send */    public void send (string message)  {         jt.send (New messagecreator ()  {    public  Message createmessage (session s)  throws jmsexception  {        textmessage msg = s.createtextmessage (message) ; return msg;    }});    }         /** receive */    public void receiver ()  {         String msg =  (String) Jt.receiveandconvert (); System.out.println ("msg===" +msg);     }}

Iv. Listeners

If you want to configure the consumer in spring, you do not need to start the receiving client again, the configuration is as follows:

<!--consumer listener--><bean id= "Jmscontainer" class= " Org.springframework.jms.listener.DefaultMessageListenerContainer "> <property name=" connectionfactory "ref="  Jmsfactory "/> <!--monitoring destination--<property name=" destination "ref=" Destinationtopic "/> <property Name= "MessageListener" ref= "MessageListener"/></bean><bean id= "MessageListener" class= " Com.liuy.spring.b.listener.mymessagelistener "></bean>
package com.liuy.spring.b.listener;import  javax.jms.jmsexception;import javax.jms.message;import javax.jms.messagelistener;import  javax.jms.textmessage;/** *  @description   Description  *  @author  liuyu *  @version  1.0 *  @date: 11:00:23 */public class mymessagelistener  April 15, 2017 morning implements messagelistener {     @Override     public  Void onmessage (message message)  {        textmessage  msg =  (TextMessage)  message;try {    system.out.println ("Receive  txt msg=== " + msg.gettext ());         } catch   (jmsexception e)  {    e.printstacktrace ();         }    }} 



This article is from "I Love Big gold" blog, please be sure to keep this source http://1754966750.blog.51cto.com/7455444/1916381

ActiveMQ (): ActiveMQ combined with spring development--the second way

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.