Hanumath
Hanumath

Reputation: 1117

How can I call externalApplication based on Activemq message using JMS

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:

enter image description here

Xml data structure.

enter image description here

What I did:

  1. I build final 3 Applications,they are working fine.

  2. 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:

  1. I want to fetch message from Activemq

  2. Find message-head from message

  3. pass message-head and xml file path to another function.Getting corresponding ApplicationRealPath value .

  4. and trigger that Application.

  5. 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

Answers (1)

Beryllium
Beryllium

Reputation: 12998

Based on these requirements

  • When data in the database is changed, a message will be posted on a queue.
  • Based on the message content you call another service dynamically.
  • That service does some computation, but it's result is not intended for an end-user. It takes 20min.

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

  • 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

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

Related Questions