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!");
}
}

}




你可能感兴趣的:(jsp,xml,servlet,jms,activemq)