- 浏览: 939798 次
- 性别:
- 来自: 江西上饶
文章分类
- 全部博客 (460)
- p.spring (56)
- p.maven (20)
- p.ant (17)
- p.jee (18)
- p.jse (33)
- p.ofbiz (31)
- p.软件工程 (8)
- p.struts2 (5)
- p.hibernate (5)
- linux (25)
- 设计模式 (2)
- p.javascript (11)
- 硬件 (1)
- p.jsp (2)
- p.windows批处理 (1)
- 操作系统问题 (5)
- 算法 (1)
- p.mysql (7)
- p.sql (5)
- p.c (1)
- google产品 (0)
- 内存 (1)
- p.struts (1)
- p.freemarker (7)
- p.css (4)
- p.log4j (10)
- p.html (3)
- 淘宝产品 (0)
- 其他 (3)
- 编译器 (0)
- svn (4)
- p.spring.security (11)
- 图形 (0)
- p.xml (1)
- p.ssh (0)
- p.jquery (4)
- p.jdbc (3)
- p.flex (0)
- p.c++ (0)
- p.c#Net (0)
- p.assembly (0)
- p.sqlserver (0)
- p.其他 (3)
- p.webwork (21)
- p.wap (12)
- p.cglib (1)
- p.jee服务器 (11)
- windows (2)
- p.iphone (1)
- p.java.分布式与集群 (2)
- p.ibatis (16)
- p.eclipse (5)
- 架构 (2)
- http协议 (5)
- 我的个人标准 (2)
- 多线程 (1)
- 奇怪问题 (5)
- p.jira (13)
- p.httpclient (1)
- 服务器.apache (11)
- 安全防范 (1)
- p.PODAM (1)
- p.junit (16)
- fop (2)
- 硬盘安装 (1)
- powerdesigner (0)
- 单元测试 (1)
- apache commons (4)
- tomcat+apache集群 (10)
- 各类诡辩 (1)
- 安卓 (8)
- qvod (1)
- java编程基础知识考试考点及答案 (0)
- 工作总结 (4)
- oracle (0)
- spring的util工具 (3)
- json (2)
- maven (3)
- jms (19)
- p.bat (3)
- hadoop (2)
- git (3)
- nginx (1)
- p.移动开发 (1)
- shiro (3)
- 游戏破解 (1)
- react-native (7)
- ios开发 (1)
- webmagic (6)
- socks5 (1)
最新评论
-
weituotian:
说的不好,没人看的
公司系统中的菜单功能和权限功能 -
石不易:
非常详细的注解~
绑定端口和IP,Listen 与VirtualHost指令 -
spring_springmvc:
spring mvc demo教程源代码下载,地址:http: ...
spring mvc -
liyixing1:
PandaDONG 写道谢谢你啊,我已经下下来了,只是还有很多 ...
jira war安装 -
liyixing1:
PandaDONG 写道谢谢你啊,我已经下下来了,只是还有很多 ...
jira war安装
在Spring中嵌入ActiveMQ有四种方式:纯Spring配置文件、在Spring的配置文件中引入ActiveMQ的配置文件、使用硬代码及ActiveMQ配置文件和在Spring配置文件中使用特定的schema。
纯Spring配置文件
在Spring的配置文件中引入ActiveMQ的配置文件
硬编码+activemq配置文件方式
使用特定的Schema
客户端配置
可以使用连接池
配置JMS消息目的地
消息消费者和消息生产者
Spring中接收消息的基本抽象机制是使用消息监听器容器(MLC: 参阅 http://mng.bz/LJti).
MLC是在消息监听器和代理之间设计的中间件,用来处理连接,线程等等,让开发者只需要关注
消息监听器中具体的业务逻辑.
同接收消息一样,Spring同样为发送消息提供了便利.Spring中发送消息的基本机制是使用JmsTemplate类.
JmsTemplate使用标准的模板模式为发送消息提供一个很方便的类.
使用Spring发送消息的一个常用方法是实现Spring的MessageCreator接口然后使用JmsTemplate类中合适
的send()方法
注意JmsTemplate使用了一个池化的连接工厂.这点很重要,因为JmsTemplate是专门设计成使用
Java EE容器的,而Java EE规范要求提供池化连接.每次调用JmsTemplate的send()方法都会创建
和销毁JMS资源(连接,消费者和生产者).因此,如果你没有使用Java EE容器,请确保在利用
JmsTemplate发送消息时使用的是池化的连接工厂.
纯Spring配置文件
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean id="admins" class="org.apache.activemq.security.AuthenticationUser"> <constructor-arg index="0" value="admin" /> <constructor-arg index="1" value="password" /> <constructor-arg index="2" value="admins,publishers,consumers" /> </bean> <bean id="publishers" class="org.apache.activemq.security.AuthenticationUser"> <constructor-arg index="0" value="publisher" /> <constructor-arg index="1" value="password" /> <constructor-arg index="2" value="publishers,consumers" /> </bean> <bean id="consumers" class="org.apache.activemq.security.AuthenticationUser"> <constructor-arg index="0" value="consumer" /> <constructor-arg index="1" value="password" /> <constructor-arg index="2" value="consumers" /> </bean> <bean id="guests" class="org.apache.activemq.security.AuthenticationUser"> <constructor-arg index="0" value="guest" /> <constructor-arg index="1" value="password" /> <constructor-arg index="2" value="guests" /> </bean> <bean id="simpleAuthPlugin" class="org.apache.activemq.security.SimpleAuthenticationPlugin"> <property name="users"> <list> <ref bean="admins" /> <ref bean="publishers" /> <ref bean="consumers" /> <ref bean="guests" /> </list> </property> </bean> <bean id="broker" class="org.apache.activemq.broker.BrokerService" init-method="start" destroy-method="stop"> <property name="brokerName" value="myBroker" /> <property name="persistent" value="false" /> <property name="transportConnectorURIs"> <list> <value>tcp://localhost:61616</value> </list> </property> <property name="plugins"> <list> <ref bean="simpleAuthPlugin" /> </list> </property> </bean> </beans>
在Spring的配置文件中引入ActiveMQ的配置文件
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 其他Spring配置 --> <bean id="broker" class="org.apache.activemq.xbean.BrokerFactoryBean"> <property name="config" value="activemq.xml" /> <property name="start" value="true"></property> </bean> </beans>
硬编码+activemq配置文件方式
public static void main(String[] args) throws Exception { String configUri = MyConfiguration3.class.getResource("/") + "activemq.xml"; System.setProperty("activemq.base", System.getProperty("user.dir")); new FileSystemXmlApplicationContext(configUri); }
使用特定的Schema
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" <span style="color:#ff0000;">xmlns:amq="http://activemq.apache.org/schema/core"</span> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd <span style="color:#ff0000;">http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd</span> "> <!-- 其他Spring配置 --> <amq:broker brokerName="myBroker" dataDirectory="${activemq.base}/data"> <amq:transportConnectors> <amq:transportConnector name="openwire" uri="tcp://localhost:61616" /> </amq:transportConnectors> <amq:plugins> <amq:simpleAuthenticationPlugin> <amq:users> <amq:authenticationUser username="admin" password="password" groups="admins,publishers,consumers" /> <amq:authenticationUser username="publisher" password="password" groups="publishers,consumers" /> <amq:authenticationUser username="consumer" password="password" groups="consumers" /> <amq:authenticationUser username="guest" password="password" groups="guests" /> </amq:users> </amq:simpleAuthenticationPlugin> </amq:plugins> </amq:broker> </beans>
客户端配置
<bean id="jmsConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://localhost:61616" /> <property name="userName" value="admin" /> <property name="password" value="password" /> </bean>
可以使用连接池
<bean id="pooledJmsConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop"> <property name="connectionFactory" ref="jmsConnectionFactory" /> </bean>
配置JMS消息目的地
<bean id="cscoDest" class="org.apache.activemq.command.ActiveMQTopic"> <constructor-arg value="STOCKS.CSCO" /> </bean> <bean id="orclDest" class="org.apache.activemq.command.ActiveMQTopic"> <constructor-arg value="STOCKS.ORCL" /> </bean>
消息消费者和消息生产者
Spring中接收消息的基本抽象机制是使用消息监听器容器(MLC: 参阅 http://mng.bz/LJti).
MLC是在消息监听器和代理之间设计的中间件,用来处理连接,线程等等,让开发者只需要关注
消息监听器中具体的业务逻辑.
<!-- 这个监听器实现需要自己编写 --> <bean id="portfolioListener" class="org.apache.activemq.book.ch3.portfolio.Listener"> </bean> <!-- Spring DMLC --> <bean id="cscoConsumer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="jmsConnectionFactory" /> <property name="destination" ref="cscoDest" /> <property name="messageListener" ref="portfolioListener" /> </bean> <!-- Spring DMLC --> <bean id="orclConsumer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="jmsConnectionFactory" /> <property name="destination" ref="orclDest" /> <property name="messageListener" ref="portfolioListener" /> </bean>
同接收消息一样,Spring同样为发送消息提供了便利.Spring中发送消息的基本机制是使用JmsTemplate类.
JmsTemplate使用标准的模板模式为发送消息提供一个很方便的类.
使用Spring发送消息的一个常用方法是实现Spring的MessageCreator接口然后使用JmsTemplate类中合适
的send()方法
public class StockMessageCreator implements MessageCreator { private int MAX_DELTA_PERCENT = 1; private Map<Destination, Double> LAST_PRICES = new Hashtable<Destination, Double>(); Destination stock; public StockMessageCreator(Destination stock) { this.stock = stock; } public Message createMessage(Session session) throws JMSException { Double value = LAST_PRICES.get(stock); if (value == null) { value = new Double(Math.random() * 100); } // lets mutate the value by some percentage double oldPrice = value.doubleValue(); value = new Double(mutatePrice(oldPrice)); LAST_PRICES.put(stock, value); double price = value.doubleValue(); double offer = price * 1.001; boolean up = (price > oldPrice); MapMessage message = session.createMapMessage(); message.setString("stock", stock.toString()); message.setDouble("price", price); message.setDouble("offer", offer); message.setBoolean("up", up); System.out.println("Sending: " + ((ActiveMQMapMessage)message).getContentMap() + " on destination: " + stock); return message; } protected double mutatePrice(double price) { double percentChange = (2 * Math.random() * MAX_DELTA_PERCENT) - MAX_DELTA_PERCENT; return price * (100 + percentChange) / 100; } }
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="pooledJmsConnectionFactory" /> </bean>
注意JmsTemplate使用了一个池化的连接工厂.这点很重要,因为JmsTemplate是专门设计成使用
Java EE容器的,而Java EE规范要求提供池化连接.每次调用JmsTemplate的send()方法都会创建
和销毁JMS资源(连接,消费者和生产者).因此,如果你没有使用Java EE容器,请确保在利用
JmsTemplate发送消息时使用的是池化的连接工厂.
发表评论
-
activemq in action 翻译
2014-11-20 10:45 828http://jackyin5918.iteye.com/ca ... -
高并发
2014-11-16 21:58 1539垂直扩展 垂直扩展是一种用于增加单个ActiveMQ代理连接数 ... -
删除不活动的队列(Delete Inactive Destinations)
2014-11-16 16:41 1324一般情况下,ActiveMQ的queue在不使用之后,可以通过 ... -
代理网络中消息传递
2014-11-13 21:21 758常见的方式是让所有客户端都连接到一个中心代理,而这不是使用消息 ... -
企业部署
2014-11-13 14:26 656主从备份方式 多个代理 ... -
REST和AJAX
2014-11-12 17:01 760REST 通过activemq提供的servlet < ... -
安全机制
2014-10-15 15:22 1200ActiveMQ中所有安全相关的概念都是通过插件的形式实现的. ... -
activemq 消息存储
2014-10-13 17:03 2308JMS规范支持两种方式分发消息:持久化和非持久化。Active ... -
ActiveMQ配置 连接activemq
2014-10-08 10:13 38977activemq代理 JMS代理(如ActiveMQ bro ... -
ActiveMQ简介
2014-10-08 09:49 745支持跨平台跨语言等特点,由于它是用JAVA实现的一套中间件,跨 ... -
jms-spring
2014-09-30 10:37 907同步接收和同步发送的情况(使用JNDI) 异步接收消息 ... -
jms-事物
2014-09-29 16:52 647JMSRedelivered可以检查消 ... -
过滤器与消息选择器
2014-09-28 23:08 670过滤器 String filter = &quo ... -
发布订阅模式
2014-09-28 11:52 997特点 1.通过话题通道进行交互。 2.可以有多个订阅者,订阅者 ... -
jms-点对点
2014-09-27 23:45 954在点对点模式中,消息创建者称为发送者,消息消费者称为接收者。 ... -
jms-message对象
2014-09-26 14:40 1169message必须通过session创建,以便完成对应的初始化 ... -
jms基础,与例子
2014-09-24 22:14 985MOM,面向消息中间件的交互模式 各个系统间,可以认为 ... -
jms的两种模式
2014-09-23 22:49 2226JMS有两种通信方式 P2P,点对点 方式和 发布/订阅模式P ...
相关推荐
利用hadoop的rpc包简单与spring结合,实现了一个简单的分布式。
1.JMX与SPRING 结合实例 2.标准MBean 与非标准MBean 注册 3.基于接口和注解的MBean 注册 4.发布通知,处理通知及配置
基于Compass2.2与Spring 结合建立索引的实例
JBPM3.2与Spring结合时任务调度的实现.doc
定时器的配置文件(两种方式:springmvc自带定时,Quartz与spring结合的定时)
Swagger与Spring结合生成Restful接口文档(该压缩包中含有相关文档和说明)
Struts2入门级教程、与Spring结合、常用标签库使用说明.
包括jmx与spring结合的例子。是新人学习和快速项目运用的例子。供大家参考学习。
hession例子,源 码,最新的jar包,还有web例子与spring结合的例子
将jmx与spring结合的jar都下载完毕2.1.1 大家可以节省时间
本例子运用了smartclient,spring框架,功能是测试tree和list组建
NULL 博文链接:https://extrimlycold20070206121610.iteye.com/blog/510814
NULL 博文链接:https://sundoctor.iteye.com/blog/360696
NULL 博文链接:https://tom-seed.iteye.com/blog/1566681
适合mybatis初学者 ,例子主要有mybatis和Spring结合在一起的使用,主要是Spring文件的配置,初学者可以参考
这是一个简单的由DWR调用Spring Bean的简单例子
apache shiro 是一个安全认证框架,和 spring security 相比,在于他使用了比较简洁易懂的认证和授权方式。...在这里主要讲解如何与 spring 结合、动态创建filterchaindefinitions、以及认证、授权、和缓存处理。
本文通过图文并茂的形式给大家介绍了mybatis-4 mybatis与spring结合使用及原理解析,非常不错,具有一定的参考借鉴价值 ,需要的朋友可以参考下
mina 与spring的结合开发 ,mina 学习,spring 学习,