简体   繁体   中英

Advice needed for JMS Queuing Scenario

Advice needed for JMS Queuing Scenario

Hi, I'm new to Java EE and Application Servers and I would like to know if a JMS Queue (or ActiveMQ?) solution is the right thing for my scenario. Right now I think not, but there seems to be a lot of options that I might not be aware.

I have client application what can access a Project (data on a database) and will lock that project until the client is finished. The client might have a lock on a project for many days if he doesn't close the application.

I need to develop a webservice that will post to a queue operation(s) to be done on a project. The hard part is that these operations need to be put on hold if this project is currently locked. (And I won't know when that lock is released; so I will need to have maybe a timer that will try to dequeue those operations).

So if the Project A is currently locked, my queue might looks like this :

  1. Add Data XXX to Project A
  2. Add Data ZZZ to Project B
  3. Update Data XXX of Project A
  4. Update Data ZZZ of Project B
  5. Remove Data ZZZ of Project B

If A is locked, i need to be able to continue to process operations on B (or any other unlocked project), and have the processing of project A later, when its lock is released.

I thought about creating in memory JMS queues for each Project that have pending operations to preserve their order) but I can't (and should not) since Queues are to be defined by the App Server. And I obviously need persistent queues (so no temporary queues ?)

Are JMS queues the right thing for this scenario ? Could ActiveMQ or another implementation help ?

Will I have to code by own Queue/SQL Table/EJB on Timer ?

There are a lot of options, both for JMS and other approaches.

But here are my thoughts of one possible solution with JMS (ActiveMQ or whatnot):

you could have a JMS queue for incomming messages and one queue for messages on hold (waiting to be processed on some locked project). Hopefully, you also have some ID (I call it "projectId", to identify a locked project). I also assume you can execute code when a project is released.

Here is some psudecode how you could think, while implementing this scenario with queues.

public void onMessage(Message msg){
   ProjData pd = extractProjectData(msg);
   if( projectLocked(pd) ) {
     msg.setStringProperty("projectId",pd.getProjectId());
     sendToOnHoldQueue(pd);
   }else{
    processProjectData(pd);
   }
}

// Say there is an event somewhere when the lock is released
public void onProjectLockReleased(projectId){
   // select messages waiting for this project via Jms selectors..

   // you may or may not want to lock the project here, while working of the "on hold events"
   MessageConsumer consumer = session.createConsumer(onHoldQueue,"projectId='"+projectId+"'");
   while(Messages msg  = consumer.receiveNoWait()){
     processProjectData(pd); 
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM