TOMCAT+ACTIVEMQ+AJAX

当activemq-web-console-5.5.0可以运行后,就开始试着添加自己的ajax的activemq的文件到客户端了。

首先是添加activemqdemo中的js文件夹的所有文件到activemq-web-console-5.5.0中的js文件夹中,然后将最简单的一个ajax的客户端文件添加到activemq-web-console-5.5.0的主页。再然后是编辑WEB-INF中的web.xml注册Ajax的servlet,最后是修改tomcat的conf中的context.xml文件注册connectionfactory,queue和topic。

web.xml添加或者修改的是

<servlet>
    <servlet-name>AjaxServlet</servlet-name>
    <servlet-class>org.apache.activemq.web.AjaxServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>AjaxServlet</servlet-name>
    <url-pattern>/amq/*</url-pattern>
  </servlet-mapping>

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/myTopic" auth="Container" type="org.apache.activemq.command.ActiveMQTopic" description="my Topic"
        factory="org.apache.activemq.jndi.JNDIReferenceFactory" physicalName="MY.NAME"/>

  <Resource name="jms/myQueue" auth="Container" type="org.apache.activemq.command.ActiveMQQueue" description="my Queue"
        factory="org.apache.activemq.jndi.JNDIReferenceFactory" physicalName="MY.NAME"/>

最简单的AJax使用方法是index.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
        <title>Ajax Test</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
         <script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
         <script type="text/javascript" src="js/amq_jquery_adapter.js"></script>
        <script type="text/javascript" src="js/amq.js"></script>
        
        <script type="text/javascript">  
       
        var amq = org.activemq.Amq;
        amq.init({ uri:'amq', logging: true,timeout: 20});
        var des = "queue://MY.NAME";
          
        var myHandler =
        {
          rcvMessage: function(message)
          {
             alert("received from handler "+message.data);
          }
        };
  
       amq.addListener('list',des,myHandler.rcvMessage);
           	
        function getRecords()
        {      
        
    	var myMessage = document.getElementById("num").value; 
     	amq.sendMessage(des, myMessage);    	
    	alert("received "+myMessage);
        }


         
        </script>
	</head>




	<body>
		title<br>
		<input type="TEXT" id="num" />
		<br>
		<br>
		<br>
		<input type="button" value="submit" onclick="getRecords()" />
		
		<br>
		<div id="recordlist" > </div>
	</body>
</html>

这时候打开http://localhost:8080/activemq-web-console-5.5.0/[your_client_file].html便可用Ajax向JMS收发消息了。

相关推荐