Spring JMS异步发收消息 ActiveMQ

JMS(使用消息中介:ActiveMQ)
JMS为JAVA开发者提供了一个与消息中介进行交互,以及发送和接收消息的标准API,而且每一个消

息中介的实现都会支持JMS。(即JMS为所有消息中介提供了统一接口);JmsTemplate是Spring消

除冗长和重复JMS代码的解决方案。JmsTemplate可以创建连接,获取会话,以及发送和接收消息。
1.在Spring中安装ActiveMQ
www.activemq.org    apache-activemq-4.1.0-incubator.zip ,把apache-activemq-4.1.0-

incubator.jar复制到lib中
启动ActiveMQ的脚本:运行刚下载的文件/bin/activemq.bat.  这样就可以使用ActiveMQ进行中介服

务了。
2.applicationContext.xml

 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">

  class="org.apache.activemq.ActiveMQConnectionFactory">








 
 
 



 



 

3.发送消息Sender.java
package com.roadrantz.marketing;

import javax.jms.Destination;
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;
   public void sendInfo()
   {
  jmsTemplate.send(new MessageCreator(){
   public Message createMessage(Session session) throws JMSException
   {   MapMessage message=session.createMapMessage();
         message.setString("lastName", "pp");
      return message;
   }
  
  });
   }
public JmsTemplate getJmsTemplate() {
 return jmsTemplate;
}
public void setJmsTemplate(JmsTemplate jmsTemplate) {
 this.jmsTemplate = jmsTemplate;
}
}
4.接收消息Receiver.java
package com.roadrantz.marketing;
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;
   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 my;
 }
 public JmsTemplate getJmsTemplate() {
  return jmsTemplate;
 }
 public void setJmsTemplate(JmsTemplate jmsTemplate) {
  this.jmsTemplate = jmsTemplate;
 }
5.测试
test.java发
package com.roadrantz.marketing;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
 public static void main(String[] args) {
  // TODO 自动生成方法存根
   ApplicationContext context = new ClassPathXmlApplicationContext

("applicationContext.xml");
   Sender sender = (Sender) context.getBean("sender");
         sender.sendInfo(); 
 }
}
test2.java收
package com.roadrantz.marketing;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test2 {
 public static void main(String[] args) {
  // TODO 自动生成方法存根
   ApplicationContext context = new ClassPathXmlApplicationContext

("applicationContext.xml");
   Receiver receiver = (Receiver) context.getBean("receiver");
   System.out.print(receiver.receiveMessage());  }

 
附件:    csdn空间4/JMS.rar
csdn空间4/apache-activemq-4.1.0-incubator.zip

应用: 站内短信息 

异步发,有目标,先发,后收,不管时间

你可能感兴趣的:(Spring,activemq,jms,spring,class,string,session)