`
gonglianying
  • 浏览: 199695 次
  • 性别: Icon_minigender_2
  • 来自: xxx
文章分类
社区版块
存档分类
最新评论

jms消息服务之ActiveMQ

阅读更多
<?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"> 
  <!-- lets create an embedded ActiveMQ Broker -->
 
  <bean id="jmsConnectionFactory"
  class="org.springframework.jndi.JndiObjectFactoryBean">
  <property name="jndiName" value="java:comp/env/jms/ConnectionFactory"/>
  </bean>
 
  <bean id="jmsQueue"
  class="org.springframework.jndi.JndiObjectFactoryBean">
  <property name="jndiName" value="java:comp/env/jms/Queue"/>
  </bean>
 
  <bean id="jmsTemplate"
  class="org.springframework.jms.core.JmsTemplate">
  <property name="connectionFactory" ref="jmsConnectionFactory"/>
  <property name="defaultDestination" ref="jmsQueue"/>
  </bean>
 
  <bean id="sender"
  class="message.Sender">
  <property name="jmsTemplate" ref="jmsTemplate"/>
  </bean>
  <bean id="receive" class="message.Receiver"/>
 
  <bean id="listenerContainer"
  class="org.springframework.jms.listener.DefaultMessageListenerContainer">
  <property name="connectionFactory" ref="jmsConnectionFactory"/>
  <property name="destination" ref="jmsQueue"/>
  <property name="messageListener" ref="receive"/>
  </bean>
</beans>

<?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">
  <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>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list> 
</web-app>

<%@ 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>

package action;

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 {

/**
*
*/
private static final long serialVersionUID = 1L;

/**
* 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
}

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();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
Sender sender = (Sender) context.getBean("sender");
String text = request.getParameter("text");
sender.send(text);
// response.encodeRedirectUrl("/index.jsp");
response.sendRedirect("/sendJms/index.jsp");
System.out.println("send....");
}

public void init() throws ServletException {
// Put your code here
}

}

package message;

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){
jmsTemplate.send(new MessageCreator(){
public Message createMessage(Session session)throws JMSException{
return session.createTextMessage(text);
}
});
}
}

package message;

import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

public class Receiver implements MessageListener {

public void onMessage(Message msg) {
if(msg instanceof TextMessage){
TextMessage text=(TextMessage)msg;
try{
System.out.println("receiver: "+text.getText());
}catch(Exception ex){
ex.printStackTrace();
}
}else{
System.out.println("warning,no TextMsg!");
}
}

}




分享到:
评论

相关推荐

    JMS之Spring +activeMQ实现消息队列

    总结起来,"JMS之Spring + ActiveMQ实现消息队列"涉及到的关键知识点包括:Spring框架的JMS支持、ActiveMQ的使用、ConnectionFactory的配置、JmsTemplate和MessageListener的实现,以及消息队列在解决系统解耦和异步...

    一头扎进JMS之ActiveMQ系列

    在提供的“一头扎进JMS之ActiveMQ视频教程”中,你将学习如何配置和使用ActiveMQ,包括安装和启动服务、创建和管理消息队列、使用生产者和消费者发送与接收消息、理解不同消息模式的工作原理,以及如何利用ActiveMQ...

    JMS.rar_activemq_jms_jms activemq

    ActiveMQ是Apache软件基金会开发的一款开源消息中间件,它是Java消息服务(Java Message Service,简称JMS)的一个实现。JMS是一种为分布式应用提供异步消息传递的API,它定义了一种标准接口,使得不同的消息系统...

    test_jms.zip_activemq_activemq案例_jms_jms test

    描述"jms简单的案例,用的activemq,使用jms前请先启动activemq服务器"表明这是一个初级的JMS实践,涉及到使用ActiveMQ作为服务器,而且在运行任何JMS相关的代码之前,需要确保ActiveMQ服务已经启动。 **ActiveMQ与...

    Spring整合Blazeds实现ActiveMQ JMS消息服务

    标题中的“Spring整合Blazeds实现ActiveMQ JMS消息服务”指的是在Java应用程序中使用Spring框架与Blazeds(一个Flex和Java之间的消息传递中间件)集成,通过ActiveMQ(一个流行的开源JMS提供商)来实现消息队列服务...

    JMS 使用 ActiveMQ 传送文件

    ActiveMQ是Apache软件基金会开发的一个开源JMS提供者,它提供了高性能、可靠的跨语言消息传递服务。 **描述:** 尽管描述中并未给出具体信息,但我们可以推断这篇博文可能详细介绍了如何使用JMS与ActiveMQ结合来...

    ActiveMQ消息服务器 v6.0.1.zip

    ActiveMQ是Apache软件基金会开发的一款开源消息中间件,它遵循开放消息传递标准(JMS,Java Message Service),用于在分布式系统中实现可靠的消息传递。在本文中,我们将深入探讨ActiveMQ v6.0.1的核心特性、应用...

    activemq与spring整合发送jms消息入门实例

    在Java世界中,ActiveMQ和Spring的整合是企业级应用中常见的消息中间件解决方案,用于实现JMS(Java Message Service)消息传递。本教程将深入探讨如何将这两个强大的工具结合在一起,以创建一个简单的发送JMS消息的...

    JMS之ActiveMQ工具类+使用例子.zip

    Apache ActiveMQ是基于Java消息服务(Java Message Service,简称JMS)的开源消息中间件,由Apache软件基金会开发。它允许应用程序通过发送和接收消息来实现异步通信,从而解耦了系统中的组件,提高了系统的可扩展性...

    jms+sping+activeMq的例子serd和recevice

    这意味着我们将深入理解如何在Spring框架中集成JMS和ActiveMQ,以实现消息的发送与接收功能。 **JMS(Java Message Service)** JMS是一种标准的API,允许Java应用程序创建、发送、接收和读取消息。它提供了异步...

    JMS教程+activemq以及activemq和tomcat的整合

    ActiveMQ是Apache软件基金会开发的一款开源、高性能、跨语言的消息中间件,它实现了JMS规范。ActiveMQ支持多种协议,如OpenWire、AMQP、STOMP、XMPP和MQTT,使得不同平台和语言的应用能够轻松地集成。ActiveMQ还提供...

    jms Spring+ActiveMQ 5.4.2

    标题 "jms Spring+ActiveMQ 5.4.2" 涉及的是Java消息服务(JMS)在Spring框架中的应用,以及ActiveMQ作为消息代理的使用。在这个主题下,我们将深入探讨JMS的基本概念、Spring对JMS的支持以及ActiveMQ 5.4.2版本的...

    一个jms activemq Topic 消息实例

    一个jms activemq Topic 消息实例 关于jms JMS 是接口,相当于jdbc ,要真正使用它需要某些厂商进行实现 ,即jms provider 常见的jms provider 有 ActiveMQ JBoss 社区所研发的 HornetQ (在jboss6 中默认即可以...

    JMS事例,ActiveMQ服务器

    ActiveMQ是Apache软件基金会开发的一个开源JMS消息代理,它实现了多种消息协议,如OpenWire、STOMP、AMQP和MQTT,为开发者提供了高度可扩展且可靠的跨语言消息传递服务。 在Java开发中,JMS和ActiveMQ的结合使用能...

    ActiveMQ-jms jar包

    ActiveMQ-jms jar包是Apache ActiveMQ项目的一部分,它提供了Java消息服务(JMS)的实现,使得开发者能够在Java应用程序中使用消息传递功能。ActiveMQ是业界广泛使用的开源消息代理,它支持多种协议,如AMQP、STOMP...

    JMS之ActiveMQ视频教程+源码

    在IT行业中,Java消息服务(Java Message Service,简称JMS)是一种标准,它定义了应用程序如何创建、发送、接收和读取消息。ActiveMQ是Apache软件基金会开发的一个开源JMS提供者,它允许开发者在分布式环境中进行...

    JMS-ActiveMQ入门实例

    Java消息服务(Java Message Service,简称JMS)是Java平台中用于创建、发送、接收和阅读消息的应用程序接口。它为应用程序提供了标准的接口,可以跨越多种消息中间件产品进行通信。JMS允许在分布式环境中交换异步...

    JMS教程+activemq以及activemq和tomcat的整合+整合实例代码+持久化消息配置以及工程+tomcat服务器的配置

    JMS简明教程+JMS规范教程+activemq以及activemq和tomcat的整合+整合实例代码+持久化消息配置以及工程+tomcat服务器的配置+整合需要的lib文件+部署多个tomcat服务器方案等

    spring整合jms+activemq

    ActivemQ是Apache软件基金会的一个项目,它实现了JMS规范,提供了一个高效、可靠的中间件服务,用于处理消息队列。本文将深入探讨如何在Spring 3.0中整合JMS与ActivemQ,以及它们在实际应用中的关键知识点。 首先,...

Global site tag (gtag.js) - Google Analytics