Reputation: 1117
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
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
.
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.
workflow:
Xml data structure.
What I did:
I build final 3 Applications,they are working fine.
I wrote a function,If you pass xmlfilepath and Message-head values as a parameters,then it returns corresponding ApplicationRealPath
tag value.
What I want to do:
I want to fetch message from Activemq
Find message-head from message
pass message-head and xml file path to another function.Getting corresponding ApplicationRealPath
value .
and trigger that Application.
fetch one more message from Activemq
,once previous triggered Application process is done
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
}
}
}
I am new to java, can you explain clearly with code.
Thanks.
Upvotes: 1
Views: 1092
Reputation: 12998
Based on these requirements
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();
}
This job runs continuously.
The differences to your proposal
main
method) instead of a servlet. A servlet is not suitable (because it does not run continuously)Update
If your services are only reachable over HTTP (this is the case with servlets/JSP), then you could implement these as a web service.
Upvotes: 2