Tomcat6.0 + ActiveMq5.1 + Spring2.5
需要的jar包(官方网下载:http://activemq.apache.org/activemq-520-release.html,其中已经包含所需要的所有包):
activemq-all-5.2.0.jar
commons-pool-1.4.jar
log4j-1.2.14.jar
spring-beans-2.5.5.jar
spring-context-2.5.5.jar
spring-core-2.5.5.jar
spring-jms-2.5.5.jar
spring-tx-2.5.5.jar
spring-web-2.5.5.jar
spring-webmvc-2.5.5.jar
xbean-spring-3.4.jar
1、applicationContext.xml(src根下):
<?xml version="1.0" encoding="UTF-8"?>
<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/schema/beans/spring-beans-2.0.xsd
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">
<bean id="jmsConnectionFactory"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jms/ConnectionFactory"></property>
</bean>
<bean id="jmsQueue"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jms/Queue"></property>
</bean>
<bean id="jmsTemplate"
class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsConnectionFactory"></property>
<property name="defaultDestination" ref="jmsQueue"></property>
</bean>
<bean id="sender" class="message.Sender">
<property name="jmsTemplate" ref="jmsTemplate"></property>
</bean>
<bean id="receive" class="message.Receiver"></bean>
<bean id="listenerContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsConnectionFactory"></property>
<property name="destination" ref="jmsQueue"></property>
<property name="messageListener" ref="receive"></property>
</bean>
</beans>
2、由于面的配置文件中使用了jndi,所以也得在Tomcat目录下的conf/context.xml配置,需加入
<Resource name="jms/ConnectionFactory"
auth="Container"
type="org.apache.activemq.ActiveMQConnectionFactory"
description="JMS Connection Factory"
factory="org.apache.activemq.jndi.JNDIReferenceFactory"
brokerURL="vm://localhost"
brokerName="LocalActiveMQBroker"/>
<Resource name="jms/Queue"
auth="Container"
type="org.apache.activemq.command.ActiveMQQueue"
description="my Queue"
factory="org.apache.activemq.jndi.JNDIReferenceFactory"
physicalName="FOO.BAR"/>
3、java类:
SendMessage.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import message.Sender;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SendMessage extends HttpServlet {
/**
* Constructor of the object.
*/
public SendMessage() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ApplicationContext xt = new ClassPathXmlApplicationContext(
"applicationContext.xml");
Sender sender = (Sender) xt.getBean("sender");
String Text = request.getParameter("text");
sender.send(Text);
response.encodeRedirectUrl("/index.jsp");
System.out.println("asdfasdf");
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
Receiver.java
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
public class Receiver implements MessageListener {
public void onMessage(Message message) {
if (message instanceof TextMessage) {
TextMessage text = (TextMessage) message;
try {
System.out.println("Receive:" + text.getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Sender.java
import javax.jms.JMSException;
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 setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
public void send(final String text){
System.out.println("---Send:"+text);
jmsTemplate.send(new MessageCreator(){
public Message createMessage(Session arg0) throws JMSException {
// TODO Auto-generated method stub
return arg0.createTextMessage(text);
}
});
}
}
4、web.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 配置启动JMS服务 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>SendMessage</servlet-name>
<servlet-class>action.SendMessage</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SendMessage</servlet-name>
<url-pattern>/servlet/SendMessage</url-pattern>
</servlet-mapping>
</web-app>
5、jsp页面,index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="servlet/SendMessage" method="post">
请输入:
<input type="text" name="text">
<br />
<input type="submit" value="确定">
</form>
</body>
</html>
分享到:
相关推荐
本文将详细阐述如何配置TOMCAT6.0、ActiveMQ5.1以及Spring2.5的集成环境,这些组件都是企业级Java应用中不可或缺的部分。 首先,我们来了解下这三个组件的基础知识: 1. **Tomcat 6.0**:Apache Tomcat 是一个开源...
**ActiveMQ5.1+Spring2.5 Demo详解** ActiveMQ是Apache软件基金会下的一个开源项目,它是一款功能强大的消息中间件,支持多种消息协议,如AMQP、STOMP、OpenWire等。在版本5.1中,ActiveMQ提供了一流的消息传输性能...
springboot +netty+activeMq在线客服系统springboot +netty+activeMq在线客服系统springboot +netty+activeMq在线客服系统springboot +netty+activeMq在线客服系统springboot +netty+activeMq在线客服系统springboot...
基于Spring+JMS+ActiveMQ+Tomcat,我使用的版本情况如下所示:Spring 3.2.0,ActiveMQ 5.4.3,Tomcat 6.0.43。本例通过详细的说明和注释,实现消息服务的基本功能:发送与接收。Spring对JMS提供了很好的支持,可以...
这通常包括`spring-boot-starter-activemq`和`org.apache.activemq:activemq-client`,以及可能的MQTT客户端库,如`org.eclipse.paho:org.eclipse.paho.client.mqttv3`。 2. 配置ActiveMQ:在`application....
spring+activemq必备jar包:activeio-core-3.1.4.jar,activemq-all-5.13.2.jar,activemq-pool-5.13.2.jar,commons-pool2-2.4.2.jar
【标题】"springmvc+dubbo/zookeeper+activemq+redis+mybatis+druid" 涵盖了多个在企业级Java应用开发中的核心技术,它们共同构建了一个高可用、高性能的服务架构。以下是对这些技术的详细解释: 1. **SpringMVC**...
基于Spring+JMS+ActiveMQ+Tomcat的整合ActiveMQSpringDemo实例源码,此实例基于Spring+JMS+ActiveMQ+Tomcat,注解的完整实例,包含jar包,可供学习及设计参考。
本篇文章将详细阐述如何在Windows操作系统上安装和配置JDK、Tomcat以及ActiveMQ,这三个组件是开发和部署Java Web应用程序的基础。 首先,我们从JDK的安装与配置开始。JDK (Java Development Kit) 是开发和运行Java...
**Spring与ActiveMQ整合详解** 在Java开发中,Spring框架是极为重要的应用基础,而ActiveMQ作为Apache出品的一款开源消息中间件,常被用于实现应用间的异步通信和解耦。本实例代码工程"Spring+ActiveMQ整合实例代码...
在IT行业中,构建高可用和负载均衡的系统是至关重要的,尤其在消息中间件领域,如ActiveMQ。本示例中的“Zookeeper+ActiveMQ测试.rar”文件包含了一个使用ZooKeeper实现ActiveMQ高可用性和负载均衡集群的实践案例。...
在"Spring+JMS+ActiveMQ+Tomcat"的组合中,Spring作为核心框架负责应用的结构和依赖管理,而JMS提供消息传递机制。ActiveMQ作为JMS的实现,承担起消息队列的职责,确保消息的可靠传输。Tomcat则作为运行环境,承载着...
JMS简明教程+JMS规范教程+activemq以及activemq和tomcat的整合+整合实例代码+持久化消息配置以及工程+tomcat服务器的配置+整合需要的lib文件+部署多个tomcat服务器方案等
基于Spring+JMS+ActiveMQ+Tomcat,做一个Spring4.1.0和ActiveMQ5.11.1整合实例,实现了Point-To-Point的异步队列消息和PUB/SUB(发布/订阅)模型,简单实例,不包含任何业务。
3、该项目采用了struts2 hibernate spring和 spring data jpa 开源框架完成,并融入了cxf开源webservice框架的应用,而这些技术都是当下流行的技术。 4、在缓存方面运用了互联网的流行技术redis实现缓存存贮,...
总结起来,"JMS之Spring + ActiveMQ实现消息队列"涉及到的关键知识点包括:Spring框架的JMS支持、ActiveMQ的使用、ConnectionFactory的配置、JmsTemplate和MessageListener的实现,以及消息队列在解决系统解耦和异步...
本文将深入探讨如何在Spring 3.0中整合JMS与ActivemQ,以及它们在实际应用中的关键知识点。 首先,我们要了解Spring对JMS的支持。Spring通过其`org.springframework.jms`包提供了丰富的JMS抽象,简化了JMS的使用。...
WebSocket 和 ActiveMQ 是两种在现代Web开发中用于实现实时通信的重要技术。WebSocket提供了一种全双工的、低延迟的通信协议,使得客户端和服务器之间可以双向实时传输数据,而ActiveMQ则是一个开源的消息中间件,常...
单片机部分采用MQTT协议将主题消息发布到队列中,java部分也采用MQTT协议进行处理,整合MQTT协议, 具体这个资源是干什么的,请查看博客: https://blog.csdn.net/qq_34178998/article/details/93158429
在IT行业中,Spring框架与ActiveMQ的结合是构建企业级应用中常见的消息中间件解决方案。Spring框架是一个开源的Java平台,它提供了丰富的功能,包括依赖注入、AOP(面向切面编程)、数据访问、Web应用以及更多的服务...