如何使用JMS基于Activemq消息调用externalApplication [英] How can I call externalApplication based on Activemq message using JMS
问题描述
我构建3个应用程序,每个应用程序需要20多分钟进行评估。我在以下目录中放置了3个文件
I build 3 Applications,each Application takes more than 20 min for evaluation.I placed 3 files in the following directories
ProjectcContextPath/WEB-INF/classes/PackageName/ExternalFileProcess.class
ProjectcContextPath/JSPFiles/index.jsp
ProjectcContextPath/WEB-INF/classes/AnotherFile.class
我想从另一个 Java
文件中调用这3个应用程序名称为 MessageConsumer.java
,基于message-head。这些消息来自 Activemq
。
I want to call these 3 Applications from another Java
file name as MessageConsumer.java
,based on message-head.These messages are fetching from Activemq
.
我不想一次获取所有消息。一旦之前完成,那么只有 MessageConsumer.java
从 Activemq
获取另一条消息。
I don't want to fetch all the messages at a time. once previous is completed then only it MessageConsumer.java
fetch another message from Activemq
.
为了更好地理解我做了一些模拟。检查它们。检查后你们会明白的。
For your better understanding I made some mock-ups.check them.After check that you guys will understand clearly.
工作流程:
Xml数据结构。
我做了什么:
-
我构建了最终的3个应用程序,它们工作正常。
I build final 3 Applications,they are working fine.
我写了一个函数,如果你传递xmlfilepath和Message-head值作为参数,那么它返回对应的 ApplicationRealPath
标签值。
I wrote a function,If you pass xmlfilepath and Message-head values as a parameters,then it returns corresponding ApplicationRealPath
tag value.
我想做什么:
-
我想从
Activemq
从混乱中找到消息头age
Find message-head from message
将message-head和xml文件路径传递给另一个函数。获取相应的 ApplicationRealPath
值。
pass message-head and xml file path to another function.Getting corresponding ApplicationRealPath
value .
并触发该应用程序。
从 Activemq
,一旦上一次触发申请流程完成
fetch one more message from Activemq
,once previous triggered Application process is done
我真的想从3天,我仍然没弄清楚。我尝试了以下代码。它无法正常工作
Really I am trying this from 3 days, still I didn't figure it out.I tried the following code.it's not working
package PackageName;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;
public class MessageConsumer extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
//creating connectionfactory object for way
ConnectionFactory connectionFactory=new
ActiveMQConnectionFactory("admin","admin","tcp://localhost:61617");
//establishing the connection b/w this Application and Activemq
Connection connection=connectionFactory.createConnection();
QueueSession session=(QueueSession) connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
Queue queue=session.createQueue("MessageTesing");
final QueueReceiver queueReceiver =session.createReceiver(queue);
connection.start();
while (true) {
Message message = queueReceiver.receive();
message.acknowledge();
TextMessage textmsg=(TextMessage) message;
if (textmsg.getText()=="TestMessage35") {
RequestDispatcher rd=request.getRequestDispatcher("servletName");
rd.forward(request, response ) ;
System.out.println(textmsg.getJMSDestination());
}
}
}
catch (Exception e) {
// TODO: handle exception
}
}
}
我是java的新手,能用代码清楚解释。
I am new to java, can you explain clearly with code.
谢谢。
推荐答案
根据这些要求
- 当数据库中的数据发生变化时,会在队列中发布消息。
- 根据消息内容动态调用其他服务。
- 该服务进行一些计算,但其结果不适用于最终用户。需要20分钟。
你可以实现一个独立的应用程序,它可以像这样同步接收消息(伪代码):
you could implement a stand-alone application which receives messages synchronously like this (pseudo code):
while (true) {
Message message = queueReceiver.receive();
TextMessage textmsg = (TextMessage) message;
if (textmsg.getText().equals("service1")) {
new Service1().execute();
} else if (textmsg.getText().equals("service2")) {
new Service2().execute();
} else {
// Show error
}
message.acknowledge();
}
此作业持续运行。
与提案的差异
- 使用独立程序(带有一个
main
方法)而不是servlet。 servlet不适合(因为它不能连续运行)。 - 将服务实现为普通的Java类;调用JSP或servlet进行处理是没用的。它们旨在实现表示层
- Using a stand-alone program (with a
main
method) instead of a servlet. A servlet is not suitable (because it does not run continuously) - Implement your services as normal Java classes; calling JSPs or servlets for processing is not useful. They are intended to implement the presentation layer
更新
如果您的服务只能通过HTTP访问(这是servlets / JSP的情况),那么您可以将它们实现为 Web服务。
If your services are only reachable over HTTP (this is the case with servlets/JSP), then you could implement these as a web service.
这篇关于如何使用JMS基于Activemq消息调用externalApplication的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!