0

I want to know if exists a framework (or any way) to wait for a message inside a java code on asynchronous way. I repeat "asynchronous way".

For example:

int a=5;
MyObject mio= new MyObject();
mio.setx(6);
String message='x|1|5ABC';

MOM.enqueue(message,'myResponseQueue'); // <-- Shoot and forget!!
String res=MOM.dequeue('myResponseQueue'); // instance of class sleep (not in memory) waiting for message.

if (a=mio.getx()){int b=6; } // code awake and continue
a=7;

message='x|2|6ABC';

MOM.enqueue(message,'myResponseQueue'); // <-- Shoot and forget again!!
String res=MOM.dequeue('myResponseQueue'); // instance of class sleep (not in memory) again waiting for message.    

if (res!=null){ // <-- code awake again
   if (a==5){int b=7;}
}

I know with a Message-Drive Bean i can create a method to wait for the message, but if I have many messages I will break the code in many methods and I will lost the values of the local variables, this makes a very ugly code.

Kaltresian
  • 961
  • 3
  • 14
  • 32

1 Answers1

0

JMS is the java (java-ee) standard way to go. Your requierements are not a problem, you could go with a MDB (and queue) per message type or a single MDB coupled with a strategy pattern to select the appropriate executor per message type.

Otherwise for an event based / reactive framework you can have a look to either akka or RxJava

Gab
  • 7,869
  • 4
  • 37
  • 68