Tomcat6.0 + ActiveMq5.1 + Spring2.5 (zhuan)
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根下):
<?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">
<beanid="jmsConnectionFactory"
class="org.springframework.jndi.JndiObjectFactoryBean">
<propertyname="jndiName"value="java:comp/env/jms/ConnectionFactory"></property>
</bean>
<beanid="jmsQueue"
class="org.springframework.jndi.JndiObjectFactoryBean">
<propertyname="jndiName"value="java:comp/env/jms/Queue"></property>
</bean>
<beanid="jmsTemplate"
class="org.springframework.jms.core.JmsTemplate">
<propertyname="connectionFactory"ref="jmsConnectionFactory"></property>
<propertyname="defaultDestination"ref="jmsQueue"></property>
</bean>
<beanid="sender"class="message.Sender">
<propertyname="jmsTemplate"ref="jmsTemplate"></property>
</bean>
<beanid="receive"class="message.Receiver"></bean>
<beanid="listenerContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<propertyname="connectionFactory"ref="jmsConnectionFactory"></property>
<propertyname="destination"ref="jmsQueue"></property>
<propertyname="messageListener"ref="receive"></property>
</bean>
</beans>
2、由于面的配置文件中使用了jndi,所以也得在Tomcat目录下的conf/context.xml配置,需加入
<Resourcename="jms/ConnectionFactory"
auth="Container"
type="org.apache.activemq.ActiveMQConnectionFactory"
description="JMSConnectionFactory"
factory="org.apache.activemq.jndi.JNDIReferenceFactory"
brokerURL="vm://localhost"
brokername="LocalActiveMQBroker"/>
<Resourcename="jms/Queue"
auth="Container"
type="org.apache.activemq.command.ActiveMQQueue"
description="myQueue"
factory="org.apache.activemq.jndi.JNDIReferenceFactory"
physicalname="FOO.BAR"/>
3、java类:
SendMessage.java
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{
/**
*Constructoroftheobject.
*/
publicSendMessage(){
super();
}
/**
*Destructionoftheservlet.<br>
*/
publicvoiddestroy(){
super.destroy();//Justputs"destroy"stringinlog
//Putyourcodehere
}
/**
*ThedoGetmethodoftheservlet.<br>
*
*Thismethodiscalledwhenaformhasitstagvaluemethodequalstoget.
*
*@paramrequest
*therequestsendbytheclienttotheserver
*@paramresponse
*theresponsesendbytheservertotheclient
*@throwsServletException
*ifanerroroccurred
*@throwsIOException
*ifanerroroccurred
*/
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();
}
/**
*ThedoPostmethodoftheservlet.<br>
*
*Thismethodiscalledwhenaformhasitstagvaluemethodequalsto
*post.
*
*@paramrequest
*therequestsendbytheclienttotheserver
*@paramresponse
*theresponsesendbytheservertotheclient
*@throwsServletException
*ifanerroroccurred
*@throwsIOException
*ifanerroroccurred
*/
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
ApplicationContextxt=newClassPathXmlApplicationContext(
"applicationContext.xml");
Sendersender=(Sender)xt.getBean("sender");
StringText=request.getParameter("text");
sender.send(Text);
response.encodeRedirectUrl("/index.jsp");
System.out.println("asdfasdf");
}
/**
*Initializationoftheservlet.<br>
*
*@throwsServletException
*ifanerroroccurs
*/
publicvoidinit()throwsServletException{
//Putyourcodehere
}
}
Receiver.java
importjavax.jms.JMSException;
importjavax.jms.Message;
importjavax.jms.MessageListener;
importjavax.jms.TextMessage;
publicclassReceiverimplementsMessageListener{
publicvoidonMessage(Messagemessage){
if(messageinstanceofTextMessage){
TextMessagetext=(TextMessage)message;
try{
System.out.println("Receive:"+text.getText());
}catch(JMSExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
}
Sender.java
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){
System.out.println("---Send:"+text);
jmsTemplate.send(newMessageCreator(){
publicMessagecreateMessage(Sessionarg0)throwsJMSException{
//TODOAuto-generatedmethodstub
returnarg0.createTextMessage(text);
}
});
}
}
4、web.xml配置:
<?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">
<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>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>
</web-app>
5、jsp页面,index.jsp
<%@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>