2 Replies Latest reply on Nov 12, 2004 5:19 PM by Kalyan

    How to achieve parallel processing in a single request?

    Anders Hedstrom Novice

      Hi all,

      I have a method in a Session EJB that will perform some business logic before it returns an answer to the client. The logic it will perform is to collect data from the applications database and two external systems, before sending all data to a third external system to get a response and send it back to the client. Each external system is quite slow so I would like to do all the collecting of data concurrent, parallel processing. How should I handle this? I'm not allowed to create my own threads in EJB's. Can I use MDB in some way? To the calling client this should be a synchronous call...

      Greatfull for any suggestions

      Cheers

      Anders =)

        • 1. Re: How to achieve parallel processing in a single request?
          Mike Finn Apprentice

          Haven't done exactly this, but it seems as though you could define two request queues, and two MDBs (one for each respective queue) to do the work. In the SLSB code, create a message for each request, set a temp replyto queue on each and push to their respective queues. Then listen on one temp queue for the answer, then the other once you get an answer on the first. You could do the msg get from the temp queues in some kind of loop, so you're not blocking on one queue too long.

          Getting the message would look something like:

          while ((!gotOne) && (!gotTwo) && (iter < max))
          {
           queueOneGetOptions.setTimeout(1000);
           queueTwoGetOptions.setTimeout(1000);
          
           try { tempQueueOne.get(msgOne); } catch (TimedOutException te) {}
           try { tempQueueTwo.get(msgTwo); } catch (TimedOutException te) {}
          
           iter++;
          }


          Seems like it would work.

          Poor man's BPM....

          HTH,
          Mike

          • 2. Re: How to achieve parallel processing in a single request?
            Kalyan Newbie

            Adding or restating to what Mike said:

            If the three queries ( to the db and to the two external applications), can happen concurrently, create 3 MDBs one for the db and one each for the two external applications. Create 3 temporary queues and send the message (or command wrapped in a message) to each of them:

            dbSender.send(queryMessage1);
            exApp1.send(queryMessage2);
            exApp2.send(queryMessage3);


            Create 3 Receivers, one each for each queue. And a message listener to all the receivers to receive the results and then process them. (You can have 3 message listeners attached, if you want to).

            Send the results, in a blocking call to the 3rd external app. Once the result is obtained, send it back to the client.

            In the SLSB code, you will have to block the control once you are done posting queries on the first three queues. This is because you wanted your code to be synchronous from client's prespective.

            HTH,

            Thanks,
            Kalyan.