Integrated ACTIVEMQ push under Spring

Source: Internet
Author: User
Tags sendmsg

This article is an implementation that integrates the ACTIVEMQ message manufacturer into spring and pushes the message through the spring background.

The first is the ApplicationContext configuration of spring, as follows

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"XMLNS:AMQ= "Http://activemq.apache.org/schema/core"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-4.1.xsd Http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/ Activemq-core.xsd ">            <!--make @autowired annotations effective -    <Beanclass= "Org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />        <!--Configure the location of the automatic scan commpent -    <Context:component-scanBase-package= "Com.test.maven.service" />        <!--Configure the target connection factory to configure the connection to the ACTIVEMQ -    <BeanID= "Targetconnectionfactory"class= "Org.apache.activemq.ActiveMQConnectionFactory">        <!--ACTIVEMQ Connection Address -        < Propertyname= "Brokerurl"value= "tcp://localhost:61616"/>        < Propertyname= "Useasyncsend"value= "true"/>    </Bean>        <!--Configure the Connection factory -    <BeanID= "Cachingconnectionfactory"class= "Org.springframework.jms.connection.CachingConnectionFactory">        <!--Number of caches -        < Propertyname= "Sessioncachesize"value= "Ten"/>        < Propertyname= "Targetconnectionfactory"ref= "Targetconnectionfactory"/>    </Bean>        <!--destination for sending messages (subscription mode) -    <BeanID= "Topicdestination"class= "Org.apache.activemq.command.ActiveMQTopic">        <!--set the name of the message subject -        <Constructor-argIndex= "0"value= "CMBC." Cmbcpushtopic " />    </Bean>    <!--destination for sending messages (peer-mode) -    <BeanID= "Queuedestination"class= "Org.apache.activemq.command.ActiveMQQueue">        <Constructor-argIndex= "0"value= "Cmbc.cmbcpushqueue"/>    </Bean>        <!--Configure the JMS template -    <BeanID= "Jmstemplate"class= "Org.springframework.jms.core.JmsTemplate">        < Propertyname= "ConnectionFactory"ref= "Cachingconnectionfactory" />        < Propertyname= "Defaultdestination"ref= "Topicdestination" />        <!--Subscription Publishing Mode -        < Propertyname= "Pubsubdomain"value= "true" />        < Propertyname= "ReceiveTimeout"value= "10000" />    </Bean>    </Beans>

Once configured, a service layer and service implementation layer are established to encapsulate the push service.

 Package Com.test.maven.service; Import javax.jms.Destination;  Public Interface activemqproducerservice{    publicvoidfinal  String msg);

Below is the specific implementation:

 PackageCom.test.maven.service.impl;ImportJavax.annotation.Resource;Importjavax.jms.Destination;Importjavax.jms.JMSException;ImportJavax.jms.Message;Importjavax.jms.Session;ImportJavax.jms.Topic;Importorg.springframework.jms.core.JmsTemplate;ImportOrg.springframework.jms.core.MessageCreator;Importorg.springframework.stereotype.Component;ImportCom.test.maven.service.ActiveMQProducerService; @Component Public classActivemqproducerserviceimplImplementsactivemqproducerservice{@Resource (name= "Jmstemplate")    Privatejmstemplate jmstemplate;  Public voidsetjmstemplate (jmstemplate jmstemplate) { This. jmstemplate =jmstemplate; }         Public voidSendmsg (Destination Destination,FinalString msg) {jmstemplate.send (destination,NewMessagecreator () { PublicMessage CreateMessage (Session session)throwsjmsexception {returnsession.createtextmessage (msg);        }        }); System.out.println ("Push String:" +msg); Try{System.out.println ("Destination:" +( (Topic) destination). Gettopicname ()); } Catch(jmsexception e) {e.printstacktrace (); }    }}

Examples of use are attached below:

 PackageCom.test.maven.controller;Importjavax.jms.Destination;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.beans.factory.annotation.Qualifier;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping;ImportCom.test.maven.service.ActiveMQProducerService; @Controller Public classsayhello{//@AutoWired is similar to @Resource function, autowired is retrieved by type in the Bean, Resoure//If you specify name to search by name, specify type to search by type, and two to specify a unique match@AutowiredPrivateActivemqproducerservice Activemqproducerservice; //when autowired retrieval is not unique, you need to filter qualifier@Autowired @Qualifier ("Topicdestination")    PrivateDestination Destination; @RequestMapping (Value= "/push")     Public voidTestpushservice (httpservletrequest requset, httpservletresponse response) {String msg= Requset.getparameter ("msg"));    Activemqproducerservice.sendmsg (destination, msg); }}

In a few days, I'll do a detailed study to embed ACTIVEMQ directly into spring so that you don't have to open activemq.

Integrated ACTIVEMQ push under Spring

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.