【问题标题】:ActiveMQ AJax ClientActiveMQ AJax 客户端
【发布时间】:2011-02-12 14:50:35
【问题描述】:

我尝试编写一个简单的 Ajax 客户端来发送和接收消息。它已成功部署,但我从未收到来自客户端的消息。我正在打败自己去思考我错过了什么,但仍然无法让它发挥作用。

这是我的代码:

  1. 我创建了一个名为 ActiveMQAjaxService 的动态 Web 应用程序,并将 activemq-web.jar 和所有必需的依赖项放在 WEB-INF/lib 文件夹中。这样AjaxServlet和MessageServlet就部署好了
  2. 我在命令行启动activemq服务器:./activemq => activemq成功创建并显示:

监听连接:tcp://lilyubuntu:61616
信息 |连接器 openwire 开始
信息 | ActiveMQ JMS 消息代理(localhost,ID:lilyubuntu-56855-1272317001405-0:0)已启动
信息 |通过 org.mortbay.log.Slf4jLog 记录到 org.slf4j.impl.JCLLoggerAdapter(org.mortbay.log)
信息 |码头-6.1.9
信息 | ActiveMQ WebConsole 已初始化。
信息 |初始化 Spring FrameworkServlet 'dispatcher'
信息 | ActiveMQ 控制台http://0.0.0.0:8161/admin
信息 |初始化 Spring 根 WebApplicationContext
信息 |连接器 vm://localhost 已启动
信息 |骆驼控制台http://0.0.0.0:8161/camel
信息 | ActiveMQ Web 演示,http://0.0.0.0:8161/demo
信息 | RESTful 文件访问应用程序http://0.0.0.0:8161/fileserver
信息 |已启动 SelectChannelConnector@0.0.0.0:8161

3) index.xml,也就是测试客户端的html:

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<script type="text/javascript" src="amq/amq.js"></script> 
<script type="text/javascript">amq.uri='amq';</script> 
<title>Hello Ajax ActiveMQ</title> 
</head> 
<body> 
<p>Hello World!</p> 
<script type="text/javascript"> 
amq.sendMessage("topic://myDetector", "message"); 
var myHandler = 
{   
  rcvMessage: function(message) 
  { 
     alert("received "+message); 
  } 
}; 

function myPoll(first) 
{ 
        if (first) 
        { 
                amq.addListener('myDetector', 'topic://myDetector', myHandler.rcvMessage); 
        } 
} 

amq.addPollHandler(myPoll);

4) Web.xml:

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License. 

-->

<display-name>ActiveMQ Web Demos</display-name> 
<description> 
Apache ActiveMQ Web Demos 
</description> 

<!-- context config --> 
<context-param> 
    <param-name>org.apache.activemq.brokerURL</param-name> 
    <param-value>vm://localhost</param-value> (I also tried tcp://localhost:61616) 
    <description>The URL of the Message Broker to connect to</description> 
</context-param> 

<context-param> 
    <param-name>org.apache.activemq.embeddedBroker</param-name> 
    <param-value>true</param-value> 
    <description>Whether we should include an embedded broker or not</description> 
</context-param> 

<!-- servlet mappings --> 

<!-- the subscription REST servlet --> 
<servlet> 
    <servlet-name>AjaxServlet</servlet-name> 
    <servlet-class>org.apache.activemq.web.AjaxServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet> 
    <servlet-name>MessageServlet</servlet-name> 
    <servlet-class>org.apache.activemq.web.MessageServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
    <!-- 
    Uncomment this parameter if you plan to use multiple consumers over REST 
    <init-param> 
            <param-name>destinationOptions</param-name> 
            <param-value>consumer.prefetchSize=1</param-value> 
    </init-param> 
    --> 

</servlet> 

<!-- the queue browse servlet --> 
<filter> 
  <filter-name>session</filter-name> 
  <filter-class>org.apache.activemq.web.SessionFilter</filter-class> 
</filter> 

<filter-mapping> 
  <filter-name>session</filter-name> 
  <url-pattern>/*</url-pattern> 
</filter-mapping> 

完成所有这些之后,我部署了 web-app,并且成功部署了,但是当我在 http://localhost:8080/ActiveMQAjaxService/index.html 中尝试时,没有任何反应。

我可以在http://localhost:8161/demo/portfolio/portfolio.html 成功运行演示组合发布器演示,并且看到数字一直在更新。但是对于我的简单网络应用程序,没有什么真正起作用的。

欢迎任何建议/提示。非常感谢

百合

【问题讨论】:

    标签: ajax web-applications client activemq


    【解决方案1】:

    尝试去管理web console;网页,并查看您是否可以从该上下文向您的代理发送消息,这可能会引导您解决问题。

    【讨论】:

      【解决方案2】:

      我在网络应用程序中设置了web.xml,如下所示,它可以工作。

      <?xml version="1.0" encoding="UTF-8"?>
      <web-app id="WebApp_ID" 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">
          <display-name>msg_tomcat_activemq</display-name>
          <context-param>
              <param-name>org.apache.activemq.brokerURL</param-name>
              <param-value>tcp://192.168.1.105:61616</param-value>
          </context-param>
      
          <context-param>
              <param-name>org.apache.activemq.embeddedBroker</param-name>
              <param-value>true</param-value>
          </context-param>
      
          <servlet>
              <servlet-name>AjaxServlet</servlet-name>
              <servlet-class>org.apache.activemq.web.AjaxServlet</servlet-class>
              <load-on-startup>1</load-on-startup>
          </servlet>
      
          <servlet>
              <servlet-name>MessageServlet</servlet-name>
              <servlet-class>org.apache.activemq.web.MessageServlet</servlet-class>
              <load-on-startup>1</load-on-startup>
      
           </servlet>
      
              <servlet-mapping>
              <servlet-name>AjaxServlet</servlet-name>
              <url-pattern>/amq/*</url-pattern>
          </servlet-mapping>
      
          <servlet-mapping>
              <servlet-name>MessageServlet</servlet-name>
              <url-pattern>/message/*</url-pattern>
          </servlet-mapping>
      
      <filter>
            <filter-name>session</filter-name>
            <filter-class>org.eclipse.jetty.continuation.ContinuationFilter</filter-class>
          </filter>
          <filter-mapping>
            <filter-name>session</filter-name>
            <url-pattern>/*</url-pattern>
          </filter-mapping>
      
          <listener>
              <listener-class>org.apache.activemq.web.SessionListener</listener-class>
          </listener>
      </web-app>
      

      【讨论】:

        猜你喜欢
        • 2014-01-26
        • 2012-07-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-26
        • 2017-02-21
        • 2016-06-05
        • 2017-07-17
        相关资源
        最近更新 更多