Integrating ACTIVEMQ under Spring

Source: Internet
Author: User
Tags message queue

1. References
    1. Spring Integrated ACTIVEMQ Configuration
    2. Spring JMS Asynchronous Send Message ActiveMQ
2. Environment in a previous ACTIVEMQ introductory example we implemented the asynchronous delivery of the message, how this blog post will integrate ACTIVEMQ in the spring environment. If you want to integrate ACTIVEMQ under Spring, you need to import the following jar package into the project:

This article has two references, so there are two examples, as shown in the project structure:

3. Example 1

Information sent by: Hellosender.java

Package edu.sjtu.erplab.springactivemq;

Import javax.jms.JMSException;
Import javax.jms.Session;

Import javax.jms.Destination;
Import Javax.jms.Message;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
Import Org.springframework.jms.core.JmsTemplate;
Import Org.springframework.jms.core.MessageCreator;

public class Hellosender {

/**
* @param args
* Both Jmstemplate and destination are formulated in the spring configuration file.
* Sender only uses the jmsfactory,jmstemplate in the configuration file, and the destination three properties
*/
public static void Main (string[] args) {
ApplicationContext ApplicationContext = new Classpathxmlapplicationcontext ("Applicationcontext-jms.xml");
Jmstemplate template = (jmstemplate) applicationcontext.getbean ("Jmstemplate");
Destination Destination = (Destination) applicationcontext.getbean ("Destination");
Template.send (Destination, new Messagecreator () {
Public Message CreateMessage (session session) throws JMSException {
Return Session.createtextmessage ("Send message: Hello ActiveMQ Text message2! ");
}
});
SYSTEM.OUT.PRINTLN ("Successfully sent a JMS message");
}
}

Information received by: Proxyjmsconsumer.java

Package edu.sjtu.erplab.springactivemq;

Import javax.jms.Destination;
Import Javax.jms.TextMessage;

Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
Import Org.springframework.jms.core.JmsTemplate;

/**
* JMS Consumer
* Content definition of the message title
* Message object after receiving Message object: Received message body * <p>
*/
public class Proxyjmsconsumer {

Public Proxyjmsconsumer () {

}
Private Jmstemplate jmstemplate;

Public Jmstemplate getjmstemplate () {
return jmstemplate;
}
public void Setjmstemplate (Jmstemplate jmstemplate) {
This.jmstemplate = jmstemplate;
}

/**
* The OnMessage (Message message) method is automatically invoked when a message is heard for the purpose of the message
*/
public void recive () {
ApplicationContext ApplicationContext = new Classpathxmlapplicationcontext ("Applicationcontext-jms.xml");
Destination Destination = (Destination) applicationcontext.getbean ("Destination");
while (true) {
try {
TextMessage txtmsg = (textmessage) jmstemplate
. receive (destination);
if (null! = txtmsg) {
System.out.println ("[DB Proxy]" + txtmsg);
System.out.println ("[DB Proxy] received message content:"
+ Txtmsg.gettext ());
} else
Break
} catch (Exception e) {
E.printstacktrace ();
}

}
}

}

Client: Jmstest.java

Package edu.sjtu.erplab.springactivemq;

Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;

public class Jmstest {

/**
* @param args
*/
public static void Main (string[] args) {
ApplicationContext ApplicationContext = new Classpathxmlapplicationcontext ("Applicationcontext-jms.xml");
Proxyjmsconsumer Proxyjmsconsumer = (proxyjmsconsumer) applicationcontext.getbean ("Messagereceiver");
Proxyjmsconsumer.recive ();

SYSTEM.OUT.PRINTLN ("Initialize message consumer");
}

}

Spring configuration file: Applicationcontext-jms.xml

<?xml version= "1.0" encoding= "UTF-8"?>
<beans xmlns= "Http://www.springframework.org/schema/beans"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context"
Xsi:schemalocation= "Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-2.5.xsd "
Default-autowire= "ByName" >


<!--configuration ConnectionFactory--
<bean id= "Jmsfactory" class= "Org.apache.activemq.pool.PooledConnectionFactory"
destroy-method= "Stop" >
<property name= "ConnectionFactory" >
<bean class= "Org.apache.activemq.ActiveMQConnectionFactory" >
<property name= "Brokerurl" >
<value>tcp://127.0.0.1:61616</value>
</property>
</bean>
</property>
<property name= "MaxConnections" value= "></property>"
</bean>

<!--Spring JMS Template---
<bean id= "Jmstemplate" class= "Org.springframework.jms.core.JmsTemplate" >
<property name= "ConnectionFactory" >
<ref local= "Jmsfactory"/>
</property>
<property name= "Defaultdestinationname" value= "Subject"/>
<!--distinguish it in a mode of false is peer to true is subscribed to
<property name= "Pubsubdomain" value= "true"/>
</bean>

<!--destination to send messages (one queue)--
<bean id= "Destination" class= "Org.apache.activemq.command.ActiveMQTopic" >
<!--set the name of the message queue--
<constructor-arg index= "0" value= "subject"/>
</bean>




<bean id= "Messagereceiver" class= "Edu.sjtu.erplab.springactivemq.ProxyJMSConsumer" >
<!--class= "Edu.sjtu.erplab.springactivemq.ProxyJMSConsumer" >-->
<property name= "Jmstemplate" ref= "Jmstemplate" ></property>
</bean>



</beans>

Test method: Run Jmstest First, then run Hellosender.

4. Example 2

Message Sender: Sender

 package edu.sjtu.erplab.springactivemq2; 

Import javax.jms.JMSException;
Import Javax.jms.MapMessage;
Import Javax.jms.Message;
Import javax.jms.Session;
Import org.springframework.jms.core.JmsTemplate;
Import Org.springframework.jms.core.MessageCreator;

public class Sender {
private jmstemplate jmstemplate;
Getter and setter
Public jmstemplate getjmstemplate () {
return jmstemplate;

public void Setjmstemplate (Jmstemplate jmstemplate) {
This.jmstemplate = jmstemplate;


public void Sendinfo () {
Jmstemplate.send (new Messagecreator () {
Public Message CreateMessage (Session session) throws JMSException {
Mapmessage message = Session.createmapmessage (); br> message.setstring ("LastName", "PPP");
return message;
}

});
}
}

Message Sending client: Sendertest

Package edu.sjtu.erplab.springactivemq2;

Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;

public class Sendertest {
public static void Main (string[] args) {
TODO automatically generate method stubs
ApplicationContext context = new Classpathxmlapplicationcontext ("Applicationcontext.xml");
Sender sender = (sender) Context.getbean ("sender");
Sender.sendinfo ();
}
}

Message receiver: Receiver

 package edu.sjtu.erplab.springactivemq2; 

Import javax.jms.JMSException;
Import Javax.jms.MapMessage;
Import org.springframework.jms.core.JmsTemplate;
Import org.springframework.jms.support.JmsUtils;

public class Receiver {
private jmstemplate jmstemplate;
Getter and setter
Public jmstemplate getjmstemplate () {
return jmstemplate;

public void Setjmstemplate (Jmstemplate jmstemplate) {
This.jmstemplate = jmstemplate;


/**
* Constructor
*/
Public Receiver () {
}

Public String receiveMessage () {
String my = "";
Mapmessage message = (mapmessage) jmstemplate.receive ();
try {
my = message.getstring ("LastName");
} catch (JMSException e) {
throw jmsutils.convertjmsaccessexception (e);
}
return to my;
}


}

Message receiving client: Receivertest

Package edu.sjtu.erplab.springactivemq2;

Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;

public class Receivertest {
public static void Main (string[] args) {
TODO automatically generate method stubs
ApplicationContext context = new Classpathxmlapplicationcontext ("Applicationcontext.xml");
Receiver receiver = (receiver) Context.getbean ("receiver");
System.out.print (Receiver.receivemessage ());
}
}

Spring configuration file: Applicationcontext.xml

<?xml version= "1.0" encoding= "UTF-8"?>
<beans
Xmlns= "Http://www.springframework.org/schema/beans"
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-2.0.xsd ">
<!--Create a connection factory--
<bean id= "ConnectionFactory"
class= "Org.apache.activemq.ActiveMQConnectionFactory" >
<property name= "Brokerurl" value= "tcp://localhost:61616" ></property>
</bean>
<!--declare a ACTIVEMQ message target, which can be a queue or a topic activemqtopic-->
<bean id= "Destination" class= "Org.apache.activemq.command.ActiveMQQueue" >
<constructor-arg index= "0" value= "edu.sjtu.erplab.springactivemq2" ></constructor-arg>
</bean>
<!---->
<bean id= "Jmstemplate" class= "Org.springframework.jms.core.JmsTemplate" >
<property name= "ConnectionFactory" ref= "ConnectionFactory" ></property>
<property name= "defaultdestination" ref= "Destination" ></property>
<property name= "ReceiveTimeout" value= "></property>"

</bean>
<bean id= "Sender" class= "Edu.sjtu.erplab.springactivemq2.Sender" >
<property name= "Jmstemplate" ref= "Jmstemplate" ></property>

</bean>
<bean id= "receiver" class= "Edu.sjtu.erplab.springactivemq2.Receiver" >
<property name= "Jmstemplate" ref= "Jmstemplate" ></property>
</bean>
</beans>
Source: Http://www.cnblogs.com/xwdreamer

Integrating ACTIVEMQ 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.