jms消息服务之ActiveMQ

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd

http://activemq.apache.org/schema/corehttp://activemq.apache.org/schema/core/activemq-core.xsd">

<!--letscreateanembeddedActiveMQBroker-->

<beanid="jmsConnectionFactory"

class="org.springframework.jndi.JndiObjectFactoryBean">

<propertyname="jndiName"value="java:comp/env/jms/ConnectionFactory"/>

</bean>

<beanid="jmsQueue"

class="org.springframework.jndi.JndiObjectFactoryBean">

<propertyname="jndiName"value="java:comp/env/jms/Queue"/>

</bean>

<beanid="jmsTemplate"

class="org.springframework.jms.core.JmsTemplate">

<propertyname="connectionFactory"ref="jmsConnectionFactory"/>

<propertyname="defaultDestination"ref="jmsQueue"/>

</bean>

<beanid="sender"

class="message.Sender">

<propertyname="jmsTemplate"ref="jmsTemplate"/>

</bean>

<beanid="receive"class="message.Receiver"/>

<beanid="listenerContainer"

class="org.springframework.jms.listener.DefaultMessageListenerContainer">

<propertyname="connectionFactory"ref="jmsConnectionFactory"/>

<propertyname="destination"ref="jmsQueue"/>

<propertyname="messageListener"ref="receive"/>

</bean>

</beans>

<?xmlversion="1.0"encoding="UTF-8"?>

<web-appversion="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>ThisisthedescriptionofmyJ2EEcomponent</description>

<display-name>ThisisthedisplaynameofmyJ2EEcomponent</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>

<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>

<%

Stringpath=request.getContextPath();

StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">

<html>

<head>

<basehref="<%=basePath%>">

<title>MyJSP'index.jsp'startingpage</title>

<metahttp-equiv="pragma"content="no-cache">

<metahttp-equiv="cache-control"content="no-cache">

<metahttp-equiv="expires"content="0">

<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">

<metahttp-equiv="description"content="Thisismypage">

<!--

<linkrel="stylesheet"type="text/css"href="styles.css">

-->

</head>

<body>

<formaction="servlet/SendMessage"method="post">

请输入:

<inputtype="text"name="text"/>

<br>

<inputtype="submit"value="确定"/>

</form>

</body>

</html>

packageaction;

importjava.io.IOException;

importjava.io.PrintWriter;

importjavax.servlet.ServletException;

importjavax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

importmessage.Sender;

importorg.springframework.context.ApplicationContext;

importorg.springframework.context.support.ClassPathXmlApplicationContext;

publicclassSendMessageextendsHttpServlet{

/**

*

*/

privatestaticfinallongserialVersionUID=1L;

/**

*Constructoroftheobject.

*/

publicSendMessage(){

super();

}

/**

*Destructionoftheservlet.<br>

*/

publicvoiddestroy(){

super.destroy();//Justputs"destroy"stringinlog

//Putyourcodehere

}

publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{

response.setContentType("text/html");

PrintWriterout=response.getWriter();

out

.println("<!DOCTYPEHTMLPUBLIC\"-//W3C//DTDHTML4.01Transitional//EN\">");

out.println("<HTML>");

out.println("<HEAD><TITLE>AServlet</TITLE></HEAD>");

out.println("<BODY>");

out.print("Thisis");

out.print(this.getClass());

out.println(",usingtheGETmethod");

out.println("</BODY>");

out.println("</HTML>");

out.flush();

out.close();

}

publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{

ApplicationContextcontext=newClassPathXmlApplicationContext(

"applicationContext.xml");

Sendersender=(Sender)context.getBean("sender");

Stringtext=request.getParameter("text");

sender.send(text);

//response.encodeRedirectUrl("/index.jsp");

response.sendRedirect("/sendJms/index.jsp");

System.out.println("send....");

}

publicvoidinit()throwsServletException{

//Putyourcodehere

}

}

packagemessage;

importjavax.jms.JMSException;

importjavax.jms.Message;

importjavax.jms.Session;

importorg.springframework.jms.core.JmsTemplate;

importorg.springframework.jms.core.MessageCreator;

publicclassSender{

privateJmsTemplatejmsTemplate;

publicvoidsetJmsTemplate(JmsTemplatejmsTemplate){

this.jmsTemplate=jmsTemplate;

}

publicvoidsend(finalStringtext){

jmsTemplate.send(newMessageCreator(){

publicMessagecreateMessage(Sessionsession)throwsJMSException{

returnsession.createTextMessage(text);

}

});

}

}

packagemessage;

importjavax.jms.Message;

importjavax.jms.MessageListener;

importjavax.jms.TextMessage;

publicclassReceiverimplementsMessageListener{

publicvoidonMessage(Messagemsg){

if(msginstanceofTextMessage){

TextMessagetext=(TextMessage)msg;

try{

System.out.println("receiver:"+text.getText());

}catch(Exceptionex){

ex.printStackTrace();

}

}else{

System.out.println("warning,noTextMsg!");

}

}

}

相关推荐