Spring JMS异步发收消息 ActiveMQ

news/2024/7/4 0:54:08 标签: activemq, jms, spring, class, string, session
class="baidu_pl">
class="article_content clearfix">
class="htmledit_views">

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

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

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

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

务了。
2.applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
 xmlns="http://www.class="tags" href="/tags/SPRING.html" title=spring>springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.class="tags" href="/tags/SPRING.html" title=spring>springframework.org/schema/beans

http://www.class="tags" href="/tags/SPRING.html" title=spring>springframework.org/schema/beans/class="tags" href="/tags/SPRING.html" title=spring>spring-beans-2.0.xsd">
<!--创建连接工厂-->
<bean id="connectionFactory"
  class="org.apache.class="tags" href="/tags/ACTIVEMQ.html" title=activemq>activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616"></property>
</bean>
<!-- 声明ActiveMQ消息目标,目标可以是一个队列,也可以是一个主题ActiveMQTopic-->
<bean id="destination" class="org.apache.class="tags" href="/tags/ACTIVEMQ.html" title=activemq>activemq.command.ActiveMQQueue">
<constructor-arg index="0" value="rantz.marketing.queue"></constructor-arg>
</bean>
<!---->
<bean id="class="tags" href="/tags/JMS.html" title=jms>jmsTemplate" class="org.class="tags" href="/tags/SPRING.html" title=spring>springframework.class="tags" href="/tags/JMS.html" title=jms>jms.core.JmsTemplate">
  <property name="connectionFactory" ref="connectionFactory"></property>
  <property name="defaultDestination" ref="destination"></property>
  <property name="receiveTimeout" value="6000"></property>

</bean>
<bean id="sender" class="com.roadrantz.marketing.Sender">
  <property name="class="tags" href="/tags/JMS.html" title=jms>jmsTemplate" ref="class="tags" href="/tags/JMS.html" title=jms>jmsTemplate"></property>

</bean>
<bean id="receiver" class="com.roadrantz.marketing.Receiver">
  <property name="class="tags" href="/tags/JMS.html" title=jms>jmsTemplate" ref="class="tags" href="/tags/JMS.html" title=jms>jmsTemplate"></property>
</bean>
</beans>

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

import javax.class="tags" href="/tags/JMS.html" title=jms>jms.Destination;
import javax.class="tags" href="/tags/JMS.html" title=jms>jms.JMSException;
import javax.class="tags" href="/tags/JMS.html" title=jms>jms.MapMessage;
import javax.class="tags" href="/tags/JMS.html" title=jms>jms.Message;
import javax.class="tags" href="/tags/JMS.html" title=jms>jms.Session;
import org.class="tags" href="/tags/SPRING.html" title=spring>springframework.class="tags" href="/tags/JMS.html" title=jms>jms.core.JmsTemplate;
import org.class="tags" href="/tags/SPRING.html" title=spring>springframework.class="tags" href="/tags/JMS.html" title=jms>jms.core.MessageCreator;
public class Sender {
 private JmsTemplate class="tags" href="/tags/JMS.html" title=jms>jmsTemplate;
   public void sendInfo()
   {
  class="tags" href="/tags/JMS.html" title=jms>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 class="tags" href="/tags/JMS.html" title=jms>jmsTemplate;
}
public void setJmsTemplate(JmsTemplate class="tags" href="/tags/JMS.html" title=jms>jmsTemplate) {
 this.class="tags" href="/tags/JMS.html" title=jms>jmsTemplate = class="tags" href="/tags/JMS.html" title=jms>jmsTemplate;
}
}
4.接收消息Receiver.java
package com.roadrantz.marketing;
import javax.class="tags" href="/tags/JMS.html" title=jms>jms.JMSException;
import javax.class="tags" href="/tags/JMS.html" title=jms>jms.MapMessage;
import org.class="tags" href="/tags/SPRING.html" title=spring>springframework.class="tags" href="/tags/JMS.html" title=jms>jms.core.JmsTemplate;
import org.class="tags" href="/tags/SPRING.html" title=spring>springframework.class="tags" href="/tags/JMS.html" title=jms>jms.support.JmsUtils;
public class Receiver {
   private JmsTemplate class="tags" href="/tags/JMS.html" title=jms>jmsTemplate;
   public Receiver() {}
   public String receiveMessage()
 {   String my="";
  MapMessage message=(MapMessage)class="tags" href="/tags/JMS.html" title=jms>jmsTemplate.receive();
    try{
     my= message.getString("lastName");
    }

    catch(JMSException e)
    {  throw JmsUtils.convertJmsAccessException(e); }
  return my;
 }
 public JmsTemplate getJmsTemplate() {
  return class="tags" href="/tags/JMS.html" title=jms>jmsTemplate;
 }
 public void setJmsTemplate(JmsTemplate class="tags" href="/tags/JMS.html" title=jms>jmsTemplate) {
  this.class="tags" href="/tags/JMS.html" title=jms>jmsTemplate = class="tags" href="/tags/JMS.html" title=jms>jmsTemplate;
 }
5.测试
test.java发
package com.roadrantz.marketing;
import org.class="tags" href="/tags/SPRING.html" title=spring>springframework.context.ApplicationContext;
import org.class="tags" href="/tags/SPRING.html" title=spring>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.class="tags" href="/tags/SPRING.html" title=spring>springframework.context.ApplicationContext;
import org.class="tags" href="/tags/SPRING.html" title=spring>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-class="tags" href="/tags/ACTIVEMQ.html" title=activemq>activemq-4.1.0-incubator.zip

应用: 站内短信息 

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


http://www.niftyadmin.cn/n/1425582.html

相关文章

Tomcat部署及负载均衡_wuli大世界_新浪博客

一、Tomcat部署 1、实施准备关闭iptables防火墙 service iptables stop安装JDK&#xff08;Java Development Kit&#xff0c;是Sun公司免费提供的Java语言的软件开发工具包&#xff0c;其中包含Java虚拟机&#xff08;JVM&#xff09;。编写好的Java源程序经过编译可形成J…

Ajax的DWR框架三在线交流

dwr.jar 1.test.html <html><head> <meta http-equiv"Content-Type" content"text/html; charsetGBK"><link rel"stylesheet" type"text/css" href"generic.css" /> <title>DWR入门--DWR后台…

页面不能保存

去掉这句就可以了哈哈 <META http-equivContent-Type content"text/html; charsetgb2312">

Tomcat部署及负载均衡

一、Tomcat部署 1、实施准备关闭iptables防火墙 service iptables stop安装JDK&#xff08;Java Development Kit&#xff0c;是Sun公司免费提供的Java语言的软件开发工具包&#xff0c;其中包含Java虚拟机&#xff08;JVM&#xff09;。编写好的Java源程序经过编译可形成J…

常见服务的默认端口_wuli大世界_新浪博客

HTTP&#xff1a;80 HTTPS&#xff1a;443 MYSQL&#xff1a;3306 SQLServer:1433 Oracle:1521FTP:21 SSH:22 Telnet:23

四DWR综合实践 用户管理模块实现

知识点&#xff1a; 编写要在前台调用的java对象&#xff1b;在dwr.xml中配置这个对象&#xff1b;在页面中引入dwr的js库及自动生成的调用java对象的js库dwr.util中常用的js函数以快速开发。 四DWR综合实践&#xff1a;用户管理模块实现 1.userManager.html<html> &…

常见服务的默认端口

HTTP&#xff1a;80 HTTPS&#xff1a;443 MYSQL&#xff1a;3306 SQLServer:1433 Oracle:1521FTP:21 SSH:22 Telnet:23

LAMP平台部署及应用_wuli大世界_新浪博客

一&#xff0c;安装PHP软件包 1&#xff0c;准备工作为避免发生程序冲突等现象&#xff0c;建议先将RPM方式安装的php及相关依赖包&#xff08;如果已存在&#xff09;卸载。例如&#xff0c;根据实际安装情况可卸载php&#xff0c;php-cli&#xff0c;php-ldap&#xff0c;php…