- 浏览: 1081512 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (1055)
- quartz定时任务 (1)
- json (9)
- 接口 (1)
- http (1)
- sccket (2)
- xmlbean (1)
- webservice (1)
- flex (1)
- sitemesh (2)
- json组装 (2)
- 存储过程 (6)
- ajax后台返回前台上下问 (3)
- flect反射机制 (1)
- 页面列表 (1)
- oracal结果集合并 (1)
- lucene (1)
- 读取配置文件工具 (1)
- 框架整合 (1)
- jms (3)
- 断点续传 (1)
- 页面之间的传值 (1)
- jpa (6)
- sql (8)
- jsp跳转 (1)
- jsp (6)
- jquery (2)
- ui (1)
- js与jsp之间的值的交互应用 (1)
- js空值判断 (1)
- Ajax (9)
- web-inf 编译目录 (1)
- 多线程 (9)
- 资料 (0)
- oracal (4)
- 多线程webservice (1)
- ztree (1)
- 加载与内容的变化 (1)
- 页面的公用变量 (1)
- web-inf目录结构 (1)
- 登陆系列 (1)
- jass应用于webservice认证 (2)
- 路径问题 (1)
- list合并 (1)
- list map中的value获取 (2)
- 标签tag (4)
- 方法 (1)
- 树形数据及其渲染 (1)
- require js 回调 (1)
- tab 页面切换 (2)
- 计时获取验证码 (1)
- 模式 (6)
- easyui (3)
- dialog (2)
- 乱码问题 (1)
- spring (29)
- sqlserver (1)
- iis,netframework (0)
- iis (1)
- netframework (1)
- 处理乱码问题 (1)
- struts-ajax (0)
- ,理论,了;,; (0)
- 排错方法 (1)
- hibernate (1)
- 二级缓存 (1)
- portal (1)
- date类型 (0)
- jvm out of memery StackOverflowError (1)
- echart数据填充 (0)
- echart (1)
- jmv (1)
- 学习 (0)
- java基础 (1)
- oracle性能优化 (1)
- 数据库三范式 (1)
- easy UI (1)
- oracle11g (1)
- form (3)
- servlet (2)
- spring mvc (1)
- htm5 (0)
- ibatise (1)
- 序列化 (1)
- tomcat双认证 (1)
- maven Lib (1)
- tomcat eclipse (2)
- ehcache (1)
- http 代替 ajax (2)
- rest (6)
- script脚本占位模板 (1)
- strust 标签 el表达式 (1)
- js bo (1)
- 公司资料 (0)
- js return (1)
- spring 第三方插件的工具类 (1)
- linux (10)
- linux 克隆 (1)
- linux xshell连接 (1)
- yum expect (1)
- linux 应用命令 (1)
- yum 缓存rmp包 (1)
- yum 本地库离线安装 (2)
- yum错误 (1)
- jsp页面用定时调用 (1)
- ajax setup (2)
- Exception,RuntimeException (1)
- 对话框 dialog (1)
- maven (17)
- PropertyPlaceholderConfigurer properties文件 (1)
- 错误集 (1)
- mybatise (0)
- 拦截器filter (1)
- http get post 区别 (1)
- rest 客户端两种返回的数据处理方式 (1)
- listener (2)
- maven工程 (1)
- ContainerResponseFilter (1)
- ajax action 打印任意实体类jsp (1)
- yum svn mvn (1)
- 分页 (1)
- angularjs (1)
- memcached (1)
- git (12)
- yyyyyyyy (0)
- oracal安装 (4)
- jaxwswebservice (1)
- spring bean (1)
- 排方法 (0)
- java 生成pdf (2)
- 创建目录 (1)
- 下载与生产pdf文件 (1)
- base64 (2)
- 配置文件 (2)
- blob (1)
- pdf (1)
- yum linux 安装Oracle (1)
- excl 插入数据库 (1)
- 搭建框架 (1)
- .classpath和jdk jre (1)
- mybatise 配置文件通配 (1)
- 用Maven插件生成Mybatis代码 (2)
- springmvc值的传递 (1)
- js jsp html (1)
- ajax跨域 (1)
- javamail (0)
- 提交复杂格式json数据 (1)
- get中文乱码/post (1)
- 弹出框,承载弹出链接----页面,显示大图 (1)
- @RequestBody list bean (1)
- 隐藏域 (1)
- 多页保存 (1)
- form 校验 (1)
- 注册,上传图片 (1)
- 登录验证 (1)
- 全局的异常处理 (0)
- 异常捕获,显示在页面 (0)
- mybatise事物配置 (1)
- maven compile (1)
- 时间格式 (1)
- js 跳转页面 (1)
- pringsecurity 角色授资源要重启服务 (0)
- Retrofi restfull (1)
- session过期推出,直接点击退出,退出 (0)
- 参数放于requestbody (1)
- maven本地仓库jar包 (1)
- 编译环境出错jdk1.6 1,7 (1)
- jsonarray ---json数组格式 (1)
- java后台文件上传,接受的2种方式 (0)
- 生成保存图片 (1)
- 权限mysql数据库Md5加密sql (1)
- mysql一对多关联查询 (1)
- mysql批量更新 (0)
- oracle in exists 区别 (1)
- https (1)
- linux发布脚本 (1)
- dubbo zookeepr (1)
- request.getParameter() (1)
- request.setAttribute() (1)
- @RequestMapping @ResponseBody (1)
- @RequestMapping @responsebody src控件 后台如何写入src值 (1)
- dwz (8)
- lib jar maven (1)
- web服务访问名称 (1)
- mvc返回内容设置,拦截 (1)
- @ResponseBody 返回json处理 (1)
- 视图,函数,存储过程 (1)
- <context:annotation-config/> (1)
- <context:component-scan/> (1)
- xml路劲通配 (2)
- 杂项 (1)
- dubbo (34)
- redis (25)
- mybatis (34)
- springmvc (15)
- js (7)
- sprinvmvc (1)
- 设计理念 (1)
- 工程 (1)
- 其他 (149)
- 导出,下载 (1)
- session (4)
- token (1)
- Exception处理 (2)
- 注解 (2)
- 框架 (3)
- shrio (1)
- login (1)
- 项目结构设计 (1)
- mybatis两种关联查询 (1)
- mybaits (2)
- 迭代模式 (1)
- oralce树形数据结构构建 (2)
- 树形结构的jsp展现 (1)
- filter intecept(Struts) (1)
- 树形结构数据的提交 (1)
- @RequestBody @RequestParam (1)
- div (1)
- equals重写 (1)
- 程序设计 (1)
- f多层for循环跳出 (1)
- 看源码 (0)
- 权限系统普遍通性 (1)
- 设计程序 (1)
- 动态拼接元素样式问题 (1)
- sql迭代 (1)
- debug断点 (1)
- spring mvc 扫描注解(ioc (1)
- di) (1)
- jdk (2)
- cglib (1)
- mybatis 多层括号(超过三层)解析不了 (1)
- tiles (2)
- cacheManager缓存的切换 (1)
- jsp,xml中el表达式等占位符中的数据操作 (1)
- jpa 及spring data jpa开发 (1)
- compareTo equals toString (1)
- 子页面用父页面js (1)
- jsp相对路径自加问题 (1)
- bean之间的相互拷贝 (1)
- DECODE (2)
- 泛型 (2)
- NVL (1)
- jsp jstl函数 用标签声明页面变量,供jstl使用,时间格式问题 (1)
- form表单切换action值 (1)
- response.getWriter().write()功能优于springMvc的返回 (1)
- 快速复制构建项目 (1)
- activeMq (15)
- cache (2)
- sql获取序列号 (1)
- 上传 (2)
- tfs (1)
- jsp标签 (2)
- qita (1)
- 触发器 (1)
- Exception (1)
- mybais (1)
- 标签 (1)
- connect by (1)
- for ... in (1)
- map转化为list (1)
- 树形结构list构建(树形实体) (1)
- 左侧菜单的设计 (1)
- response.getWriter().write() (1)
- eclipse打断点之后断无效 (1)
- 直接访问/web-inf/下的页面 (1)
- clob (1)
- freemark (1)
- 框架的设计 (1)
- get方式处理乱码 (1)
- Request的getParameter和getAttribute方法的区别 (1)
- cas (9)
- 时间格式问题 (1)
- ResponseUtil.writeToResponse (1)
- 树形数据的反选 (1)
- spring data jpa (1)
- jsp异常提示 (1)
- jquery js (1)
- eclipse (1)
- 乱码 (1)
- Json OBJETC (1)
- PROCEDURE (1)
- pl/sql oracle (1)
- 设计 (1)
- el表达式 (3)
- iframe (1)
- map (1)
- jsp中调用Java (1)
- response.getWriter().write() ajax (1)
- mybatis xml 传入参数 (0)
- response与request在返回时作用区别 (1)
- spring cache (1)
- 模型驱动 (1)
- 关于mybatis传空值的处理 (1)
- 日志配置 (1)
- mapper (1)
- mapper mybatis (1)
- mapper mybatis-spring spring-data-jpa (1)
- js插件 (1)
- spring-data-jpa (2)
- 字节流转化过程 (1)
- 数据库 (11)
- jsp页面 (1)
- 局域网络访问问题 (1)
- plsql (2)
- response (1)
- plsq (1)
- Proxool (1)
- cas security (1)
- 系统配置化 (1)
- 集群 (4)
- springboot (24)
- 设计模式 (5)
- 通知 (1)
- 架构 (48)
- zookeeper (5)
- mvc框架 (1)
- 事物隔离策略 (1)
- jvm (8)
- 调研 (1)
- Java工具类 (1)
- 并发容器 (3)
- 多线程管理器 (4)
- 简历 (0)
- 查询 (1)
- 集群工具 (1)
- springMcv (1)
- ConcurrentHashMap (1)
- hashtable (1)
- cac (1)
- 树形结构 (1)
- 定时任务 (1)
- tortoiseGit (2)
- struts (11)
- shiro (31)
- log4j (3)
- struts2 (2)
- 编码 (1)
- request (3)
- Nginx (14)
- tomcat (1)
- idea (12)
- mvc (2)
- BeanUtils (2)
- image (1)
- mino (1)
- httpClient (1)
- volecity (1)
- swagger (3)
- 调错 (0)
- data (1)
- log (1)
- shell (1)
- 事物 (3)
- junit (1)
- RestTemplate (2)
- 线程 (0)
- Exception异常处理 (1)
- Exception异常注解 (1)
- lock (1)
- HashMap (1)
- 面试 (0)
- rabbitMq (1)
- rainCat (1)
- 其它 (1)
- activemq消息传送机制以及ACK机制详解 (0)
- mysql (7)
- fildder (1)
- UML (1)
- 校验 (1)
- 反射 (1)
- 切面 (1)
- springioc (1)
- urule2 (0)
- skywalking (1)
- urule (2)
- docker (2)
- 前端 (1)
- bootstrap (1)
- eureka (2)
- springcloud (16)
- jenkins (2)
- springsecurity (1)
- 数据结构 (4)
- gradle (1)
- mycat (3)
- nacos (1)
- canary (1)
- 线程池 (1)
- solr (1)
- resteasy (2)
- BI (1)
- dfs (2)
- elasticsearch (1)
- ruby (1)
- logstash (1)
- clickhouse (2)
- davinci (15)
- java (1)
- davinvi (1)
- fastdfs (1)
- vue (1)
- 《将博客搬至CSDN》 (1)
最新评论
-
nizhipeng123:
private SessionFactory sessio ...
用map接收参数的几个注意的问题
==============发送方配置(与接受方配置差别在于不要配置服务器)=======
http://hellohank.iteye.com/blog/1341290
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:amq="http://activemq.org/config/1.0"
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.xsd
http://activemq.org/config/1.0 http://activemq.apache.org/schema/core/activemq-core-5.0.0.xsd">
<!-- 配置ActiveMQ服务 -->
<amq:broker useJmx="false" persistent="false">
<amq:transportConnectors>
<!-- 提供的连接方式有:VM Transport、TCP Transport、SSL Transport、
Peer Transport、UDP Transport、Multicast Transport、HTTP and HTTPS Transport、
Failover Transport、Fanout Transport、Discovery Transport、ZeroConf Transport等 -->
<amq:transportConnector uri="tcp://127.0.0.1:61616" />
</amq:transportConnectors>
</amq:broker>
<!-- 配置JMS连接工厂(注:brokerURL是关键,
它应该是上面的amq:transportConnectors里面的值之一) -->
<amq:connectionFactory id="jmsConnectionFactory"
brokerURL="tcp://127.0.0.1:61616" />
<!-- 消息发送的目的地(注:amq:queue是用于指定是发送topic不是queue,对应上面配置中的amq:destinations) -->
<amq:queue name="destination" physicalName="ossQueue" />
<!-- 创建JMS的Session生成类 -->
<bean id="jmsTemplate"
class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<bean
class="org.springframework.jms.connection.SingleConnectionFactory">
<property name="targetConnectionFactory"
ref="jmsConnectionFactory" />
</bean>
</property>
<!-- 指定发送信息时使用的消息转换类.
这个选项不填的话,默认的是:SimpleMessageConverter,它只支持4种类型的对象:String, byte[],Map,Serializable
-->
<!-- 如果加上下面这段配置就会出错, 错误原因是Book不是一个原始类, 但我已经将它继承Serializable了,可还是不行, 我想可能有其他什么原因吧, 但我现在不清楚 -->
<!-- <property name="messageConverter"
ref="resourceMessageConverter" /> -->
</bean>
<!-- 发送消息的转换类
(这个类要继承org.springframework.jms.support.converter.MessageConverter) -->
<bean id="resourceMessageConverter"
class="com.focustech.jms.ResourceMessageConverter" />
<!-- 消息生产者(通过指定目的地, 就可以同时指定其发送的消息模式是topic还是queue) -->
<bean id="resourceMessageProducer"
class="com.focustech.jms.ResourceMessageProducer">
<property name="template" ref="jmsTemplate" />
<property name="destination" ref="destination" />
</bean>
<!-- 消息接收类(这个类需要继承,当然也可以通过MessageListenerAdapter指定消息转换器来实现用户自定义的消息收发) -->
<bean id="resourceMessageListener"
class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
<constructor-arg>
<bean
class="com.focustech.jms.ResourceMessageConsumer">
</bean>
</constructor-arg>
<property name="defaultListenerMethod" value="recieve" />
<!-- 自定义接收类与接收的方法 -->
<property name="messageConverter"
ref="resourceMessageConverter" />
</bean>
<!-- 消息监听容器,其各属性的意义为:
connectionFactory:指定所监听的对象,在这里就是监听连接到tcp://127.0.0.1:61616上面的ActiveMQ;
destination:监听的消息模式;
messageListener:接收者
-->
<bean id="listenerContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsConnectionFactory" />
<property name="destination" ref="destination" />
<property name="messageListener" ref="resourceMessageListener" />
</bean>
</beans>
===========接收方配置============
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:amq="http://activemq.org/config/1.0"
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.xsd
http://activemq.org/config/1.0 http://activemq.apache.org/schema/core/activemq-core-5.0.0.xsd">
<!-- 配置JMS连接工厂(注:brokerURL是关键,
它应该是上面的amq:transportConnectors里面的值之一) -->
<amq:connectionFactory id="jmsConnectionFactory"
brokerURL="tcp://127.0.0.1:61616" />
<!-- 消息发送的目的地(注:amq:queue是用于指定是发送topic不是queue,对应上面配置中的amq:destinations) -->
<amq:queue name="destination" physicalName="ossQueue" />
<!-- 创建JMS的Session生成类 -->
<bean id="jmsTemplate"
class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<bean
class="org.springframework.jms.connection.SingleConnectionFactory">
<property name="targetConnectionFactory"
ref="jmsConnectionFactory" />
</bean>
</property>
<!-- 指定发送信息时使用的消息转换类.
这个选项不填的话,默认的是:SimpleMessageConverter,它只支持4种类型的对象:String, byte[],Map,Serializable
-->
<!-- 如果加上下面这段配置就会出错, 错误原因是Book不是一个原始类, 但我已经将它继承Serializable了,可还是不行, 我想可能有其他什么原因吧, 但我现在不清楚 -->
<!-- <property name="messageConverter"
ref="resourceMessageConverter" /> -->
</bean>
<!-- 发送消息的转换类
(这个类要继承org.springframework.jms.support.converter.MessageConverter) -->
<bean id="resourceMessageConverter"
class="com.focustech.jms.ResourceMessageConverterb" />
<!-- 消息生产者(通过指定目的地, 就可以同时指定其发送的消息模式是topic还是queue) -->
<bean id="resourceMessageProducer"
class="com.focustech.jms.ResourceMessageProducerb">
<property name="template" ref="jmsTemplate" />
<property name="destination" ref="destination" />
</bean>
<!-- 消息接收类(这个类需要继承,当然也可以通过MessageListenerAdapter指定消息转换器来实现用户自定义的消息收发) -->
<bean id="resourceMessageListener"
class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
<constructor-arg>
<bean
class="com.focustech.jms.ResourceMessageConsumerb">
</bean>
</constructor-arg>
<property name="defaultListenerMethod" value="recieve" />
<!-- 自定义接收类与接收的方法 -->
<property name="messageConverter"
ref="resourceMessageConverter" />
</bean>
<!-- 消息监听容器,其各属性的意义为:
connectionFactory:指定所监听的对象,在这里就是监听连接到tcp://127.0.0.1:61616上面的ActiveMQ;
destination:监听的消息模式;
messageListener:接收者
-->
<bean id="listenerContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsConnectionFactory" />
<property name="destination" ref="destination" />
<property name="messageListener" ref="resourceMessageListener" />
</bean>
</beans>
==========web.xml=====
<!-- spring配置 -->
<context-param>
<param-name>spring.profiles.default</param-name>
<param-value>development</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml,classpath*:*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
=====java===
package com.focustech.jms;
import java.io.Serializable;
public class Book implements Serializable
{
/**
*
*/
private static final long serialVersionUID = -6988445616774288928L;
long id;
String name;
String author;
public String getAuthor()
{
return author;
}
public void setAuthor(String author)
{
this.author = author;
}
public long getId()
{
return id;
}
public void setId(long id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
package com.focustech.jms;
public class ResourceMessageConsumer
{
public void recieve(Object obj)
{
Book book = (Book) obj;
System.out.println("============qqqqq===========================");
System.out.println("receiveing message qqqq...");
System.out.println(book.toString());
System.out.println("here to invoke our business methodqqqq...");
System.out.println("====================qqq===================");
}
}
package com.focustech.jms;
import java.awt.print.Book;
import java.util.HashMap;
import java.util.Map;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import org.apache.activemq.command.ActiveMQObjectMessage;
import org.springframework.jms.support.converter.MessageConversionException;
import org.springframework.jms.support.converter.MessageConverter;
public class ResourceMessageConverter implements MessageConverter
{
@SuppressWarnings("unchecked")
public Message toMessage(Object obj, Session session) throws JMSException, MessageConversionException
{
// check Type
if (obj instanceof Book)
{
// 采用ActiveMQ的方式传递消息
ActiveMQObjectMessage objMsg = (ActiveMQObjectMessage) session.createObjectMessage();
Map map = new HashMap();
map.put("Book", obj);
// objMsg.setObjectProperty里面放置的类型只能是:String, Map, Object, List
objMsg.setObjectProperty("book", map);
return objMsg;
}
else
{
throw new JMSException("Object:[" + obj + "] is not Book");
}
}
public Object fromMessage(Message msg) throws JMSException, MessageConversionException
{
if (msg instanceof ObjectMessage)
{
Object obj = ((ObjectMessage) msg).getObject();
return obj;
}
else
{
throw new JMSException("Msg:[" + msg + "] is not Map");
}
}
}
package com.focustech.jms;
import javax.jms.Destination;
import org.springframework.jms.JmsException;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class ResourceMessageProducer
{
private JmsTemplate template;
private Destination destination;
public JmsTemplate getTemplate()
{
return template;
}
public void setTemplate(JmsTemplate template)
{
this.template = template;
}
public Destination getDestination()
{
return destination;
}
public void setDestination(Destination destination)
{
this.destination = destination;
}
public void send(Book book)
{
System.out.println("=======================================");
System.out.println("do send ......");
long l1 = System.currentTimeMillis();
template.convertAndSend(this.destination, book);
System.out.println("send time:" + (System.currentTimeMillis() - l1) / 1000 + "s");
System.out.println("=======================================");
}
}
==jsp==
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils" %>
<%@ page import="org.springframework.web.context.WebApplicationContext" %>
<%@ page import="org.springframework.jms.JmsException" %>
<%@ page import="com.focustech.jms.Book" %>
<%@ page import="com.focustech.jms.ResourceMessageProducer" %>
<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils" %>
<%
try {
ServletContext servletContext = this.getServletContext();
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
ResourceMessageProducer resourceMessageProducer = (ResourceMessageProducer) wac.getBean("resourceMessageProducer");
Book book = new Book();
book.setId(123);
book.setName("jms test!");
book.setAuthor("taofucheng");
resourceMessageProducer.send(book);
} catch (JmsException e) {
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
集成事务
Spring提供的JMS的API中已经有了集成事务的功能,我们只要将上面监听容器的配置改成下面的就行了:
首先,将jmsTemplate设置成支持事务(它默认是不支持事务的):
<bean id="jmsTemplate"
class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<bean
class="org.springframework.jms.connection.SingleConnectionFactory">
<property name="targetConnectionFactory"
ref="jmsConnectionFactory" />
</bean>
</property>
<property name="sessionTransacted" value="true"/>
</bean>
然后再在消息监听容器中设置指定的事务管理:
<bean id="listenerContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsConnectionFactory" />
<property name="destination" ref="destination" />
<property name="messageListener" ref="resourceMessageListener" />
<!—jtaTransactionManager是系统中的事务管理类,在我们的系统中,是由Spring托管的 -->
<property name="transactionManager" ref="jtaTransactionManager" />
</bean>
这样配置之后,当事务发生回滚时,消息也会有回滚,即不发送出去。
4、其它高级应用
ActiveMQ还有许多其它高级的应用,如:自动重连机制,也就是保证当通信双方或多方的链接断裂后它会根据用户的设置自动连接,以保证建立可靠的传输;另外,ActiveMQ还有其它方式嵌入到Spring中,如它可以通过xbean, file等方式建立应用;它还可以通过JMX对消息的发送与接收进行实时查看;消息的确认方式等等,还有很多高级的应用,请参考
http://hellohank.iteye.com/blog/1341290
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:amq="http://activemq.org/config/1.0"
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.xsd
http://activemq.org/config/1.0 http://activemq.apache.org/schema/core/activemq-core-5.0.0.xsd">
<!-- 配置ActiveMQ服务 -->
<amq:broker useJmx="false" persistent="false">
<amq:transportConnectors>
<!-- 提供的连接方式有:VM Transport、TCP Transport、SSL Transport、
Peer Transport、UDP Transport、Multicast Transport、HTTP and HTTPS Transport、
Failover Transport、Fanout Transport、Discovery Transport、ZeroConf Transport等 -->
<amq:transportConnector uri="tcp://127.0.0.1:61616" />
</amq:transportConnectors>
</amq:broker>
<!-- 配置JMS连接工厂(注:brokerURL是关键,
它应该是上面的amq:transportConnectors里面的值之一) -->
<amq:connectionFactory id="jmsConnectionFactory"
brokerURL="tcp://127.0.0.1:61616" />
<!-- 消息发送的目的地(注:amq:queue是用于指定是发送topic不是queue,对应上面配置中的amq:destinations) -->
<amq:queue name="destination" physicalName="ossQueue" />
<!-- 创建JMS的Session生成类 -->
<bean id="jmsTemplate"
class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<bean
class="org.springframework.jms.connection.SingleConnectionFactory">
<property name="targetConnectionFactory"
ref="jmsConnectionFactory" />
</bean>
</property>
<!-- 指定发送信息时使用的消息转换类.
这个选项不填的话,默认的是:SimpleMessageConverter,它只支持4种类型的对象:String, byte[],Map,Serializable
-->
<!-- 如果加上下面这段配置就会出错, 错误原因是Book不是一个原始类, 但我已经将它继承Serializable了,可还是不行, 我想可能有其他什么原因吧, 但我现在不清楚 -->
<!-- <property name="messageConverter"
ref="resourceMessageConverter" /> -->
</bean>
<!-- 发送消息的转换类
(这个类要继承org.springframework.jms.support.converter.MessageConverter) -->
<bean id="resourceMessageConverter"
class="com.focustech.jms.ResourceMessageConverter" />
<!-- 消息生产者(通过指定目的地, 就可以同时指定其发送的消息模式是topic还是queue) -->
<bean id="resourceMessageProducer"
class="com.focustech.jms.ResourceMessageProducer">
<property name="template" ref="jmsTemplate" />
<property name="destination" ref="destination" />
</bean>
<!-- 消息接收类(这个类需要继承,当然也可以通过MessageListenerAdapter指定消息转换器来实现用户自定义的消息收发) -->
<bean id="resourceMessageListener"
class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
<constructor-arg>
<bean
class="com.focustech.jms.ResourceMessageConsumer">
</bean>
</constructor-arg>
<property name="defaultListenerMethod" value="recieve" />
<!-- 自定义接收类与接收的方法 -->
<property name="messageConverter"
ref="resourceMessageConverter" />
</bean>
<!-- 消息监听容器,其各属性的意义为:
connectionFactory:指定所监听的对象,在这里就是监听连接到tcp://127.0.0.1:61616上面的ActiveMQ;
destination:监听的消息模式;
messageListener:接收者
-->
<bean id="listenerContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsConnectionFactory" />
<property name="destination" ref="destination" />
<property name="messageListener" ref="resourceMessageListener" />
</bean>
</beans>
===========接收方配置============
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:amq="http://activemq.org/config/1.0"
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.xsd
http://activemq.org/config/1.0 http://activemq.apache.org/schema/core/activemq-core-5.0.0.xsd">
<!-- 配置JMS连接工厂(注:brokerURL是关键,
它应该是上面的amq:transportConnectors里面的值之一) -->
<amq:connectionFactory id="jmsConnectionFactory"
brokerURL="tcp://127.0.0.1:61616" />
<!-- 消息发送的目的地(注:amq:queue是用于指定是发送topic不是queue,对应上面配置中的amq:destinations) -->
<amq:queue name="destination" physicalName="ossQueue" />
<!-- 创建JMS的Session生成类 -->
<bean id="jmsTemplate"
class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<bean
class="org.springframework.jms.connection.SingleConnectionFactory">
<property name="targetConnectionFactory"
ref="jmsConnectionFactory" />
</bean>
</property>
<!-- 指定发送信息时使用的消息转换类.
这个选项不填的话,默认的是:SimpleMessageConverter,它只支持4种类型的对象:String, byte[],Map,Serializable
-->
<!-- 如果加上下面这段配置就会出错, 错误原因是Book不是一个原始类, 但我已经将它继承Serializable了,可还是不行, 我想可能有其他什么原因吧, 但我现在不清楚 -->
<!-- <property name="messageConverter"
ref="resourceMessageConverter" /> -->
</bean>
<!-- 发送消息的转换类
(这个类要继承org.springframework.jms.support.converter.MessageConverter) -->
<bean id="resourceMessageConverter"
class="com.focustech.jms.ResourceMessageConverterb" />
<!-- 消息生产者(通过指定目的地, 就可以同时指定其发送的消息模式是topic还是queue) -->
<bean id="resourceMessageProducer"
class="com.focustech.jms.ResourceMessageProducerb">
<property name="template" ref="jmsTemplate" />
<property name="destination" ref="destination" />
</bean>
<!-- 消息接收类(这个类需要继承,当然也可以通过MessageListenerAdapter指定消息转换器来实现用户自定义的消息收发) -->
<bean id="resourceMessageListener"
class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
<constructor-arg>
<bean
class="com.focustech.jms.ResourceMessageConsumerb">
</bean>
</constructor-arg>
<property name="defaultListenerMethod" value="recieve" />
<!-- 自定义接收类与接收的方法 -->
<property name="messageConverter"
ref="resourceMessageConverter" />
</bean>
<!-- 消息监听容器,其各属性的意义为:
connectionFactory:指定所监听的对象,在这里就是监听连接到tcp://127.0.0.1:61616上面的ActiveMQ;
destination:监听的消息模式;
messageListener:接收者
-->
<bean id="listenerContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsConnectionFactory" />
<property name="destination" ref="destination" />
<property name="messageListener" ref="resourceMessageListener" />
</bean>
</beans>
==========web.xml=====
<!-- spring配置 -->
<context-param>
<param-name>spring.profiles.default</param-name>
<param-value>development</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml,classpath*:*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
=====java===
package com.focustech.jms;
import java.io.Serializable;
public class Book implements Serializable
{
/**
*
*/
private static final long serialVersionUID = -6988445616774288928L;
long id;
String name;
String author;
public String getAuthor()
{
return author;
}
public void setAuthor(String author)
{
this.author = author;
}
public long getId()
{
return id;
}
public void setId(long id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
package com.focustech.jms;
public class ResourceMessageConsumer
{
public void recieve(Object obj)
{
Book book = (Book) obj;
System.out.println("============qqqqq===========================");
System.out.println("receiveing message qqqq...");
System.out.println(book.toString());
System.out.println("here to invoke our business methodqqqq...");
System.out.println("====================qqq===================");
}
}
package com.focustech.jms;
import java.awt.print.Book;
import java.util.HashMap;
import java.util.Map;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import org.apache.activemq.command.ActiveMQObjectMessage;
import org.springframework.jms.support.converter.MessageConversionException;
import org.springframework.jms.support.converter.MessageConverter;
public class ResourceMessageConverter implements MessageConverter
{
@SuppressWarnings("unchecked")
public Message toMessage(Object obj, Session session) throws JMSException, MessageConversionException
{
// check Type
if (obj instanceof Book)
{
// 采用ActiveMQ的方式传递消息
ActiveMQObjectMessage objMsg = (ActiveMQObjectMessage) session.createObjectMessage();
Map map = new HashMap();
map.put("Book", obj);
// objMsg.setObjectProperty里面放置的类型只能是:String, Map, Object, List
objMsg.setObjectProperty("book", map);
return objMsg;
}
else
{
throw new JMSException("Object:[" + obj + "] is not Book");
}
}
public Object fromMessage(Message msg) throws JMSException, MessageConversionException
{
if (msg instanceof ObjectMessage)
{
Object obj = ((ObjectMessage) msg).getObject();
return obj;
}
else
{
throw new JMSException("Msg:[" + msg + "] is not Map");
}
}
}
package com.focustech.jms;
import javax.jms.Destination;
import org.springframework.jms.JmsException;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class ResourceMessageProducer
{
private JmsTemplate template;
private Destination destination;
public JmsTemplate getTemplate()
{
return template;
}
public void setTemplate(JmsTemplate template)
{
this.template = template;
}
public Destination getDestination()
{
return destination;
}
public void setDestination(Destination destination)
{
this.destination = destination;
}
public void send(Book book)
{
System.out.println("=======================================");
System.out.println("do send ......");
long l1 = System.currentTimeMillis();
template.convertAndSend(this.destination, book);
System.out.println("send time:" + (System.currentTimeMillis() - l1) / 1000 + "s");
System.out.println("=======================================");
}
}
==jsp==
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils" %>
<%@ page import="org.springframework.web.context.WebApplicationContext" %>
<%@ page import="org.springframework.jms.JmsException" %>
<%@ page import="com.focustech.jms.Book" %>
<%@ page import="com.focustech.jms.ResourceMessageProducer" %>
<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils" %>
<%
try {
ServletContext servletContext = this.getServletContext();
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
ResourceMessageProducer resourceMessageProducer = (ResourceMessageProducer) wac.getBean("resourceMessageProducer");
Book book = new Book();
book.setId(123);
book.setName("jms test!");
book.setAuthor("taofucheng");
resourceMessageProducer.send(book);
} catch (JmsException e) {
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
集成事务
Spring提供的JMS的API中已经有了集成事务的功能,我们只要将上面监听容器的配置改成下面的就行了:
首先,将jmsTemplate设置成支持事务(它默认是不支持事务的):
<bean id="jmsTemplate"
class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<bean
class="org.springframework.jms.connection.SingleConnectionFactory">
<property name="targetConnectionFactory"
ref="jmsConnectionFactory" />
</bean>
</property>
<property name="sessionTransacted" value="true"/>
</bean>
然后再在消息监听容器中设置指定的事务管理:
<bean id="listenerContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsConnectionFactory" />
<property name="destination" ref="destination" />
<property name="messageListener" ref="resourceMessageListener" />
<!—jtaTransactionManager是系统中的事务管理类,在我们的系统中,是由Spring托管的 -->
<property name="transactionManager" ref="jtaTransactionManager" />
</bean>
这样配置之后,当事务发生回滚时,消息也会有回滚,即不发送出去。
4、其它高级应用
ActiveMQ还有许多其它高级的应用,如:自动重连机制,也就是保证当通信双方或多方的链接断裂后它会根据用户的设置自动连接,以保证建立可靠的传输;另外,ActiveMQ还有其它方式嵌入到Spring中,如它可以通过xbean, file等方式建立应用;它还可以通过JMX对消息的发送与接收进行实时查看;消息的确认方式等等,还有很多高级的应用,请参考
相关推荐
中文名: 经典Java EE企业应用实战--基于WebLogic/JBoss的JSF+EJB 3+JPA整合开发 原名: 经典Java EE企业应用实战--基于WebLogic/JBoss的JSF+EJB 3+JPA整合开发 作者: 李刚 资源格式: PDF 版本: 第一版 出版社: 电子...
" " "支持对系统运行状态、Web应用、EJB组件、数据库连接" " "池、交易服务、JMS服务及其它服务端组件与服务进行 " " "动态监控与管理。 " " "Web应用和ear的部署和管理也可以通过Web管理控制台 " " "进行,可以在...
实例119 Request-Reply模式的JMS应用 421 实例120 使用Java IDL 426 实例121 EJB与CORBA的交互 430 实例122 基于EJB的真实世界模型 433 实例123 EJB的商业应用——定购单 447 第11章 Java 2 Platform Micro Edition...
Reply模式的JMS应用 421 实例120 使用Java IDL 426 实例121 EJB与CORBA的交互 430 实例122 基于EJB的真实世界模型 433 实例123 EJB的商业应用——定购单 447 第11章 Java 2 Platform Micro Edition...
EntityBean被用来代表应用系统中用到的数据。 对于客户机,SessionBean是一种非持久性对象,它实现某些在服务器上运行的业务逻辑。 对于客户机,EntityBean是一种持久性对象,它代表一个存储在持久性存储器中的...
双主架构 主从同步 读写分离 性能优化架构能力 代码级别 关联代码优化 cache对其 分支预测 copy on write 内联优化 系统优化 cache 延迟计算 数据预读 异步 轮询与通知 内存池 模块化 工程架构...
EntityBean被用来代表应用系统中用到的数据。 对于客户机,SessionBean是一种非持久性对象,它实现某些在服务器上运行的业务逻辑。 对于客户机,EntityBean是一种持久性对象,它代表一个存储在持久性存储器中的实体...
Java生成密钥、保存密钥的实例源码,通过本源码可以了解到Java如何产生单钥加密的密钥(myKey)、产生双钥的密钥对(keyPair)、如何保存公钥的字节数组、保存私钥到文件privateKey.dat、如何用Java对象序列化保存私钥...
Java生成密钥、保存密钥的实例源码,通过本源码可以了解到Java如何产生单钥加密的密钥(myKey)、产生双钥的密钥对(keyPair)、如何保存公钥的字节数组、保存私钥到文件privateKey.dat、如何用Java对象序列化保存私钥...
应用程序 (MQ Application)................................................................. 28 1.3 工作原理...............................................................................................
1.6.2. 预订系统概况................................................................................................................................................... 48 1.6.3. 理解Seam 业务对话...