package org.camunda.bpm.example.asynchronous_messaging_jms;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.inject.Named;
import javax.jms.Connection;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.QueueConnectionFactory;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.runtime.Execution;
import org.camunda.bpm.engine.runtime.ProcessInstance;

@Named
@Stateless
public class CallbackService {

  @Inject
  private RuntimeService runtimeService;
  
  @PersistenceContext
  private EntityManager entityManager;
  
  @Resource(mappedName = "java:/queue/test")
  private Queue queue;

  @Resource(mappedName = "java:/JmsXA")
  private QueueConnectionFactory connectionFactory;  
  
  public void receiveCallback(String correlationKey) {
//    String executionId = correlationKey;
//    System.out.println("do callback on execution " + executionId);
    
    
//    Execution execution = runtimeService.createExecutionQuery()
//            .processVariableValueEquals("correllationId", correlationKey)
//            .singleResult();

//    Execution execution = runtimeService.createExecutionQuery()
//            .processInstanceBusinessKey(correlationKey)
//            .singleResult();
    
//    runtimeService.signal(execution.getId(), variables);
	  
//	  Map<String,Object> correlationKeys = new HashMap<String, Object>();
//	  correlationKeys.put("correllationId", correlationKey);
//	  runtimeService.correlateMessage("serviceExecutionCompleted", correlationKeys);
	  
	  runtimeService.createMessageCorrelation("serviceExecutionCompleted")
	  	.processInstanceVariableEquals("correllationId", correlationKey)
	  	.setVariable("payload", "somePayload")
	  	.correlate();
	  
  }
  
  public void triggerAsynchronousCallback(String asynchronousCorrelationKey) {
    try {
      Connection connection = connectionFactory.createConnection();
      Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
      MessageProducer producer = session.createProducer(queue);
      TextMessage message = session.createTextMessage(asynchronousCorrelationKey);
      producer.send(message);
      producer.close();
      session.close();
      connection.close();
    }
    catch (Exception ex) {
      throw new RuntimeException("Could not send JMS message", ex);
    }
  }

}

@@ -1034,130 +1034,8 @@
 ) %7B%0A
-//    String executionId = correlationKey;%0A//    System.out.println(%22do callback on execution %22 + executionId);%0A    %0A    %0A
 //  
@@ -1088,32 +1088,32 @@
 xecutionQuery()%0A
+
 //            .p
@@ -1203,17 +1203,19 @@
 sult();%0A
+//
 %0A
-
 //    Ex
@@ -1357,24 +1357,26 @@
 leResult();%0A
+//
     %0A//    r
@@ -1423,24 +1423,115 @@
 variables);%0A
+//    runtimeService.messageEventReceived(%22serviceExecutionCompleted%22, execution.getId());%0A
 %09  %0A//%09  Map

Improve demo code