- 浏览: 3302 次
- 性别:
- 来自: 广州
最近访客 更多访客>>
文章分类
最新评论
-
liwei:
严重: Exception sending context i ...
ActiveMQ5.0.1+Spring实现JMS异步消息发送 -
meng9999:
以上的代码是可以跑起来的;
ActiveMQ5.0.1+Spring实现JMS异步消息发送 -
endeavor416:
LZ,好像那个XML的配置文件在我这里允许的时候有问题呢?请问 ...
ActiveMQ5.0.1+Spring实现JMS异步消息发送 -
HRoger:
支持,最近偶也在研究!谢过
ActiveMQ5.0.1+Spring实现JMS异步消息发送
先到[url=http://www.activemq.org]http://www.activemq.org[/url](官网)上面去下载ActiveMQ5.0.1. 把一些毕要的jar包加入到项目的lib目录下面.
至于jar包的添加在官方网上都有介绍.
官方网上的资料信息会给你的开发带来很大的帮助.这是个人的感触虽然是全英文的.但哪些英语并不难.
下面是Spring的配置文件.
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:amq="http://activemq.apache.org/schema/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schem /beans/spring-beans-2.0.xsd http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd http://activemq.apache.org/camel/schema/spring http://activemq.apache.org/camel/schema/spring /camel-spring.xsd"> <!-- 数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@192.168.0.100:1521:ora10" /> <property name="username" value="ceipportal" /> <property name="password" value="ceipportal" /> </bean> <!-- Spring中的jdbc模型 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource"> <ref bean="dataSource" /> </property> </bean> <!-- 配置connectionFactory --> <bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop"> <property name="connectionFactory"> <bean class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL"> <value>tcp://localhost:61616</value> </property> </bean> </property> </bean> <!-- Spring JMS Template --> <bean id="myJmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory"> <ref local="jmsFactory" /> </property> <property name="defaultDestinationName" value="subject" /> <property name="messageConverter" ref="conversion" /> <!-- 区别它采用的模式为false是p2p为true是订阅 --> <property name="pubSubDomain" value="false" /> </bean> <!-- 读取信息 --> <bean id="consumer" class="org.spring.activeDemo01.Consumer"> <property name="jdbcTemplate" ref="jdbcTemplate" /> <property name="jmsTemplate" ref="myJmsTemplate" /> </bean> <!-- 发送信息 --> <bean id="producer" class="org.spring.activeDemo01.Producer"> <property name="jmsTemplet" ref="myJmsTemplate" /> <!-- <property name="destination" ref="destination"/>--> <!-- <property name="destinationName" value="subject"/> --> </bean> <!-- 消息监听 --> <bean id="listenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="concurrentConsumers" value="5" /> <property name="connectionFactory" ref="jmsFactory" /> <property name="destinationName" value="subject" /> <property name="messageListener" ref="consumer" /> </bean> <!-- 消息转换 --> <bean id="conversion" class="org.spring.activeDemo01.PaySettlementCoverter" /> <!-- POJO类 --> <bean id="hello" class="org.spring.activeDemo01.Hello" /> </beans>
发送信息类
package org.spring.activeDemo01; import org.springframework.jms.core.JmsTemplate; public class Producer{ private JmsTemplate jmsTemplet = null ; public void setJmsTemplet(JmsTemplate jmsTemplet) { this.jmsTemplet = jmsTemplet; } public void simpleSend(Hello h) { this.jmsTemplet.setDeliveryPersistent(true); this.jmsTemplet.convertAndSend(h); } }
接收信息类
public class Consumer implements MessageListener{ private JdbcTemplate jdbcTemplate; private JmsTemplate jmsTemplate; private static final String insert_sql = "insert into jms_queue_send (id, MessageID, MessageDetails) values (seq_jms_queue_id.nextval, ?,?)"; public JdbcTemplate getJdbcTemplate() { return jdbcTemplate; } public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public void onMessage(Message arg0) { ActiveMQObjectMessage msg = (ActiveMQObjectMessage)arg0; try { System.out.print("From -->"+msg.getStringProperty("id")); System.out.println("-->"+msg.getStringProperty("hello")); /*String insert_sql = "insert into jms_queue_send (id, MessageID, MessageDetails)" + " values (seq_jms_queue_id.nextval, '"+msg.getJMSMessageID()+"', '"+msg.getStringProperty("hello")+"')"; jdbcTemplate.execute(insert_sql);*/ } catch (JMSException e) { e.printStackTrace(); } } public JmsTemplate getJmsTemplate() { return jmsTemplate; } public void setJmsTemplate(JmsTemplate jmsTemplate) { this.jmsTemplate = jmsTemplate; } }
消息转换类
package org.spring.activeDemo01; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.ObjectMessage; import javax.jms.Session; import org.springframework.jms.support.converter.MessageConversionException; import org.springframework.jms.support.converter.MessageConverter; public class PaySettlementCoverter implements MessageConverter{ public Object fromMessage(Message message) throws JMSException, MessageConversionException { ObjectMessage objMsg = (ObjectMessage)message; Hello hello = new Hello(); hello.setId(objMsg.getStringProperty("id")); hello.setSaying(objMsg.getStringProperty("hello")); return hello; } public Message toMessage(Object obj, Session session) throws JMSException, MessageConversionException { Hello hello = (Hello)obj; ObjectMessage objMsg = session.createObjectMessage(); objMsg.setJMSCorrelationID("123654"); objMsg.setJMSReplyTo(objMsg.getJMSDestination()); objMsg.setStringProperty("id",hello.getId()); objMsg.setStringProperty("hello",hello.getSaying()); return objMsg; } }
POJO类
public class Hello { /** * */ private static final long serialVersionUID = 1L; private String id; private String saying; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getSaying() { return saying; } public void setSaying(String saying) { this.saying = saying; } }
测试类
import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestSender { /** * @param args */ public static void main(String[] args) { ClassPathXmlApplicationContext c = new ClassPathXmlApplicationContext("appliactionContext.xml"); Producer d = (Producer)c.getBean("producer"); Hello h = (Hello)c.getBean("hello"); h.setId("123456789"); h.setSaying("Hello World.....!!! "); d.simpleSend(h); } }
评论
4 楼
liwei
2008-11-30
严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 9 in XML document from ServletContext resource [/WEB-INF/classes/applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'beans'.
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:369)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:313)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:290)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:142)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:158)
at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:124)
at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:92)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:97)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:411)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:338)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:251)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:190)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3827)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4334)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:920)
at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:883)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:492)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1138)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:516)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:566)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
Caused by: org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'beans'.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:78)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:361)
... 36 more
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 9 in XML document from ServletContext resource [/WEB-INF/classes/applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'beans'.
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:369)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:313)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:290)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:142)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:158)
at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:124)
at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:92)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:97)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:411)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:338)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:251)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:190)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3827)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4334)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:920)
at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:883)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:492)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1138)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:516)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:566)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
Caused by: org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'beans'.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:78)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:361)
... 36 more
3 楼
meng9999
2008-11-25
以上的代码是可以跑起来的;
2 楼
endeavor416
2008-11-24
LZ,好像那个XML的配置文件在我这里允许的时候有问题呢?请问楼主是在什么环境下跑的?用了哪些包?谢谢!!!
1 楼
HRoger
2008-08-25
支持,最近偶也在研究!谢过
相关推荐
在实际的"ActiveMQ+Camel+Spring+jms Demo"项目中,开发者可能首先会在Spring配置中定义一个JMS模板,用于发送消息;然后,创建一个消息监听器,由Spring容器管理,用于接收和处理消息。Camel可以通过其强大的路由...
在本项目"ActiveMQ+Camel+Spring+jms Demo(二)"中,我们将探讨一个集成ActiveMQ消息中间件、Apache Camel路由引擎、Spring框架以及Java消息服务(JMS)的示例应用。这个组合提供了强大的企业级消息处理能力,是...
在本文中,我们将深入探讨如何使用SpringBoot、ActiveMQ和MQTT来实现消息的发送与接收。这是一个典型的分布式系统中的消息通信场景,其中SpringBoot作为应用程序框架,ActiveMQ作为消息中间件,而MQTT(Message ...
将ActiveMQ与Spring结合,可以轻松地在Spring应用中集成消息队列,实现高效、解耦的系统通信。 在这个"ActiveMQ5.1+Spring2.5 Demo"中,我们将深入探讨如何在Spring 2.5版本中配置和使用ActiveMQ,以及如何构建一个...
基于Spring+JMS+ActiveMQ+Tomcat,我使用的版本情况如下所示:Spring 3.2.0,ActiveMQ 5.4.3,Tomcat 6.0.43。本例通过详细的说明和注释,实现消息服务的基本功能:发送与接收。Spring对JMS提供了很好的支持,可以...
因此,通过ActiveMQ实现邮件异步发送,可以将这个任务放入消息队列,由后台服务负责处理。这样,前端可以快速返回结果,而邮件发送则在后台按顺序完成。 在项目的压缩包中,有三个主要的子文件: 1. activemq.zip:...
"JMS之Spring + ActiveMQ实现消息队列"涉及到的关键知识点包括:Spring框架的JMS支持、ActiveMQ的使用、ConnectionFactory的配置、JmsTemplate和MessageListener的实现,以及消息队列在解决系统解耦和异步处理中的...
综上所述,"activeMQ+spring整合"是一个实用的示例,它演示了如何在Spring环境中配置和使用ActiveMQ,以实现消息的可靠传输。通过下载并研究`activemqDOME`这个压缩包中的代码,你可以更深入地理解这个整合过程,...
### Spring+JMS+ActiveMQ+Tomcat 实现消息服务 #### 一、技术栈介绍 在本案例中,我们采用的技术栈为Spring 2.5、ActiveMQ 5.4.0 和 Tomcat 6.0.30。这些技术的结合能够有效地构建一个可靠的消息传递系统。 - **...
在Java世界中,ActiveMQ和Spring的整合是企业级应用中常见的消息中间件解决方案,用于实现JMS(Java Message Service)消息传递。本教程将深入探讨如何将这两个强大的工具结合在一起,以创建一个简单的发送JMS消息的...
通过它可以方便地发送同步或异步消息,而无需直接处理JMS API。 5. **消息监听器** Spring提供了一个`MessageListener`接口,通过实现该接口,可以在接收到消息时自动执行特定的操作。此外,`...
这个"activeMQ+spring+springmvc整合示例"旨在帮助开发者理解如何将这三者结合,以实现消息队列(Message Queue)的功能,提高系统的异步处理能力和负载均衡。以下是关于这一整合的详细知识讲解: 1. **ActiveMQ**:...
标题“jms+activeMq+spring学习简单例子”表明这个压缩包包含了一些示例代码,用于演示如何在Spring框架中集成JMS和ActiveMQ,以便于理解和学习。通过这个例子,开发者可以了解如何在实际应用中实现基于消息的通信。...
总结起来,"Spring+JMS+ActiveMQ+Tomcat"的组合为企业级应用提供了一套高效、可靠的异步消息处理方案。Spring简化了开发流程,JMS提供了消息传输的标准,ActiveMQ保证了消息传递的稳定,而Tomcat则提供了运行环境。...
ActiveMQ是Apache软件基金会开发的一款开源消息中间件,它遵循Java消息服务(JMS)标准,提供高效、可靠的异步消息传递。在分布式系统中,消息中间件扮演着至关重要的角色,它允许不同组件之间通过解耦的方式进行...
通过`Spring`的容器管理和配置,我们可以轻松地集成`ActiveMQ`,实现异步通信,提高系统的性能和稳定性。在实际项目中,这种组合是构建分布式系统、微服务架构的重要工具,值得开发者深入学习和掌握。
基于Spring+JMS+ActiveMQ+Tomcat的整合ActiveMQSpringDemo实例源码,此实例基于Spring+JMS+ActiveMQ+Tomcat,注解的完整实例,包含jar包,可供学习及设计参考。