CDI Injection and waiting i.e. Thread.sleep()

Hi everbody,

I wan´t to wait for a message to be available. I tried something like:

while (subscription == null && (System.currentTimeMillis() - startTime) < 30000) { try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } subscription = runtimeService .createEventSubscriptionQuery() .processInstanceId(instanceId) .eventType("message") .eventName(meassageTaskName.toString()) .singleResult(); }

It works but it looks very ugly. Is there some other trick, or a better pattern. The runtimeService is injected via CDI.

THX

You could attach an ExecutionListener to the corresponding Message Event in the process and notify your bean as the message becomes available. Be aware of the transacitonal semantics, though: the message will only be available in the database after the transaction is committed. If you are a CDI aficionado, then you could do it in an event driven way with a transactional observer which is invoked after commit.

However: your polling solution is not necessarily bad. It has the advantage that it is very robust: if you use a notification mechanism you need to account for the case when the handler cannot process the notificaiton successfully. The polling will inherently retry. However: it may be more efficient to not poll messages for each process instance individually but use a single poller thread (scheduled EJB) and then dispatch the results to some handlers.

Daniel, thx much for this very interesting ideas. I will investigate a few things. I think to do something with ExecutionListener is little more elegant.

grettings from Frankonia