Summarize with spring + ActiveMQ

Source: Internet
Author: User

Spring Consolidated JMS-based ACTIVEMQ implementation of message sending and receiving read a lot of documents on the Internet, and finally summed up their own needs. First, download and install ACTIVEMQ First we go to the Apache official website to download activemq (http://activemq.apache.org/download.html), After unpacking, run the Activemq.bat file under its bin directory to start Activemq. second, the spring to join the ActiveMQ configuration first copy the relevant jar to the project's lib file look at the relevant catalogs before you configure them for easy understanding start configuration below

<!--ActiveMQ Connection factory--
<!--can actually generate connection connectionfactory, provided by the corresponding JMS service provider--
<bean id= "Connectinfactory" class= "Org.apache.activemq.ActiveMQConnectionFactory" >
<!--<property name= "Brokerurl" value= "tcp://192.168.1.79:61616"/>--
<property name= "Brokerurl" value= "${mqurl}"/>
</bean>
<!--Spring Caching connection factory--
<!--spring for managing real connectionfactory connectionfactory--
<bean id= "Cachingconnectionfactory"
class= "Org.springframework.jms.connection.CachingConnectionFactory" >
<!--target ConnectionFactory corresponds to the real connectionfactory that can generate the JMS connection
<property name= "Targetconnectionfactory" ref= "Connectinfactory" ></property>
<!--session Cache number--
<property name= "Sessioncachesize" value= "ten" ></property>
</bean>

<!--Configure how messages are sent to destination-
<!--queue: Only one subscriber will receive the message, and once the message is processed there will be no queue--

<bean id= "Notifyqueue" class= "Org.apache.activemq.command.ActiveMQQueue" >
<constructor-arg value= "Q.notify" ></constructor-arg>
</bean>
<!--destination: Topic theme: Put a message, all subscribers will receive--
<!--This is a theme destination, one-to-many
<bean id= "Notifytopic" class= "Org.apache.activemq.command.ActiveMQTopic" >
<constructor-arg value= "T.notify" ></constructor-arg>
</bean>
<!--Spring JMS template To configure JMS templates--
<bean id= "Jmstemplate" class= "Org.springframework.jms.core.JmsTemplate" >
<property name= "ConnectionFactory" ref= "Cachingconnectionfactory"/>
</bean>
<!--Use the Spring jmstemplate message producer--
<bean id= "Queuemessageproducer" class= "Com.common.jms.QueueMessageProducer" >
<property name= "Jmstemplate" ref= "Jmstemplate" ></property>
<property name= "Notifyqueue" ref= "Notifyqueue" ></property>
<property name= "Messageconverter" ref= "Messageconverter" ></property>
</bean>
<bean id= "Topicmessageproducer" class= "Com.common.jms.TopicMessageProducer" >
<property name= "Jmstemplate" ref= "Jmstemplate" ></property>
<property name= "Notifytopic" ref= "Notifytopic" ></property>
<property name= "Messageconverter" ref= "Messageconverter" ></property>
</bean>
<!--message consumers generally use spring's MDP to receive queue mode asynchronously--
<!--message monitoring container--
<bean id= "Queuecontainer"
class= "Org.springframework.jms.listener.DefaultMessageListenerContainer" >
<property name= "ConnectionFactory" ref= "Connectinfactory" ></property>
<property name= "Destination" ref= "Notifyqueue" ></property>
<property name= "MessageListener" ref= "Queuemessagelistener" ></property>
</bean>
<!--message monitoring container--
<bean id= "Topiccontainer"
class= "Org.springframework.jms.listener.DefaultMessageListenerContainer" >
<property name= "ConnectionFactory" ref= "Connectinfactory" ></property>
<property name= "Destination" ref= "Notifytopic" ></property>
<property name= "MessageListener" ref= "Topicmessagelistener" ></property>
<!--Publish Subscription mode--
<property name= "Pubsubdomain" value= "true"/>

</bean>
<!--asynchronous Receive Message processing class-
<bean id= "Queuemessagelistener" class= "Com.common.jms.QueueMessageListener" >
<property name= "Messageconverter" ref= "Messageconverter" ></property>
</bean>
<bean id= "Topicmessagelistener" class= "Com.common.jms.TopicMessageListener" >
<property name= "Messageconverter" ref= "Messageconverter" ></property>
</bean>
<bean id= "Messageconverter" class= "Com.common.jms.NotifyMessageConverter" >
</bean>

Here's a look at sender

public class Sender {
 private static ServletContext ServletContext;
 private static Webapplicationcontext ctx; 
 /**
  * Send point-to-point information
  * @param Noticeinfo
  */
 public static void Setqueuesender () { 
  servletcontext = Servletactioncontext.getservletcontext ();
  ctx = Webapplicationcontextutils.getwebapplicationcontext (ServletContext);
   queuemessageproducer notifymessageproducer = (( Queuemessageproducer) Ctx.getbean ("Queuemessageproducer"));
   phonenoticeinfo noticeinfo = new Phonenoticeinfo ();

(The following shows Phonenoticeinfo and then Queuemessageproducer )
Noticeinfo.setnoticecontent ("Hello Word");
Noticeinfo.setnoticetitle ("Hello Word");
Noticeinfo.setreceiver ("Hello");
Noticeinfo.setreceiverphone ("1111111");
Notifymessageproducer.sendqueue (Noticeinfo);
}

public static ServletContext Getservletcontext () {
return ServletContext;
}
public static void Setservletcontext (ServletContext servletcontext) {
Sender.servletcontext = ServletContext;
}
public static Webapplicationcontext Getctx () {
return CTX;
}
public static void Setctx (Webapplicationcontext ctx) {
Sender.ctx = CTX;
}
}

Phonenoticeinfo

public class Phonenoticeinfo implements Serializable {
 /** Message title * /
 public String noticetitle;
 /** Message Contents */
 public String noticecontent;
 /** receiver */
 public String receiver;
 /** Receive Phone number */
 public String receiverphone;
 public String Getnoticetitle () {
  return noticetitle;
 }
 public void Setnoticetitle (String noticetitle) {
  this.noticetitle = noticetitle;
 }
 public String getnoticecontent () {
  return noticecontent;
 }
 public void Setnoticecontent (String noticecontent) {
  this.noticecontent = Noticecontent;
 }
 public String getreceiver () {
  return receiver;
 }
 public void Setreceiver (String receiver) {
  this.receiver = receiver;
 }

Public String Getreceiverphone () {
return receiverphone;
}
public void Setreceiverphone (String receiverphone) {
This.receiverphone = Receiverphone;
}

}

Queuemessageproducer

/**
* Message producer Service class
*/
public class Queuemessageproducer {
Private Jmstemplate jmstemplate;
Private Destination Notifyqueue;
Private Notifymessageconverter Messageconverter;
public void Sendqueue (Phonenoticeinfo noticeinfo) {
SendMessage (Noticeinfo);
}
private void SendMessage (Phonenoticeinfo noticeinfo) {
TODO auto-generated Method Stub
Jmstemplate.setmessageconverter (Messageconverter);
Jmstemplate.setpubsubdomain (FALSE);
Jmstemplate.convertandsend (Notifyqueue,noticeinfo);
}
Public Jmstemplate getjmstemplate () {
return jmstemplate;
}
public void Setjmstemplate (Jmstemplate jmstemplate) {
This.jmstemplate = jmstemplate;
}
Public Destination Getnotifyqueue () {
return notifyqueue;
}
public void Setnotifyqueue (Destination notifyqueue) {
This.notifyqueue = Notifyqueue;
}
Public Notifymessageconverter Getmessageconverter () {
return messageconverter;
}
public void Setmessageconverter (Notifymessageconverter messageconverter) {
This.messageconverter = Messageconverter;
}
}

Notifymessageconverter

/**
* Message Conversion
*/
public class Notifymessageconverter implements Messageconverter {
private static Logger Logger = Loggerfactory.getlogger (Notifymessageconverter.class);
@Override
/**
* Convert received message to Noticeinfo object
*/
Public Object frommessage (Message message) throws JMSException,
messageconversionexception {
TODO auto-generated Method Stub
if (logger.isdebugenabled ()) {
Logger.debug ("Receive JMS message:" +message);
}
if (message instanceof ObjectMessage) {
ObjectMessage omsg = (objectmessage) message;
if (omsg instanceof activemqobjectmessage) {
Activemqobjectmessage amsg = (activemqobjectmessage) omsg;
try {
Phonenoticeinfo Noticeinfo = (phonenoticeinfo) amsg.getobject ();
return noticeinfo;
} catch (Exception e) {
Todo:handle exception
Logger.error ("message:${} is not a instance of Noticeinfo." +message.tostring ());
throw new JMSException ("Message:" +message.tostring () + "is not a instance of Noticeinfo." +message.tostring ());
}
}else{
Logger.error ("message:${} is not a instance of Activemqobjectmessage." +message.tostring ());
throw new JMSException ("Message:" +message.tostring () + "is not a instance of Activemqobjectmessage." +message.tostring ());
}
}else {
Logger.error ("message:${} is not a instance of ObjectMessage." +message.tostring ());
throw new JMSException ("Message:" +message.tostring () + "is not a instance of ObjectMessage." +message.tostring ());
}
}

@Override
/**
* Convert Noticeinfo object to message
*/
Public Message tomessage (Object obj, session session) throws JMSException,
messageconversionexception {
TODO auto-generated Method Stub
if (logger.isdebugenabled ()) {
Logger.debug ("Convert Notify object to JMS message:${}" +obj.tostring ());
}
if (obj instanceof phonenoticeinfo) {
Activemqobjectmessage msg = (activemqobjectmessage) session.createobjectmessage ();
Msg.setobject ((phonenoticeinfo) obj);
return msg;
}else {
Logger.debug ("Convert Notify object to JMS message:${}" +obj.tostring ());
}
return null;
}

}

Queuemessagelistener

public class Queuemessagelistener implements MessageListener {
private static Logger Logger = Loggerfactory.getlogger (Queuemessagelistener.class);
Private Notifymessageconverter Messageconverter;

/**
* Receive messages
*/
@Override
public void OnMessage (message message) {
TODO auto-generated Method Stub
try {
ObjectMessage objectmessage = (objectmessage) message;
Phonenoticeinfo Noticeinfo = (phonenoticeinfo) messageconverter.frommessage (objectmessage);
System.out.println ("Queue received message" +noticeinfo.getnoticecontent ());
SYSTEM.OUT.PRINTLN ("Model:" +objectmessage.getjmsdeliverymode ());
SYSTEM.OUT.PRINTLN ("Destination:" +objectmessage.getjmsdestination ());
System.out.println ("Type:" +objectmessage.getjmstype ());
System.out.println ("MessageId:" +objectmessage.getjmsmessageid ());
System.out.println ("Time:" +objectmessage.getjmstimestamp ());
System.out.println ("ExpiredTime:" +objectmessage.getjmsexpiration ());
System.out.println ("Priority:" +objectmessage.getjmspriority ());

} catch (Exception e) {
Todo:handle exception
Logger.error ("Exception occurred while processing information", e);
}
}
Public Notifymessageconverter Getmessageconverter () {
return messageconverter;
}
public void Setmessageconverter (Notifymessageconverter messageconverter) {
This.messageconverter = Messageconverter;
}

}

Summarize with spring + ActiveMQ

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.