package org.camunda.bpm.example.asynchronous_messaging_jms;

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);  
  }
  
  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);
    }
  }

}

@@ -54,16 +54,65 @@
 g_jms;%0A%0A
+import java.util.HashMap;%0Aimport java.util.Map;%0A%0A
 import j
@@ -1148,24 +1148,26 @@
 ;%0A    %0A    %0A
+//
     Executio
@@ -1218,16 +1218,18 @@
 Query()%0A
+//
         
@@ -1294,16 +1294,18 @@
 ionKey)%0A
+//
         
@@ -1482,24 +1482,26 @@
 ult();%0A    %0A
+//
     runtimeS
@@ -1544,18 +1544,231 @@
 iables);
-  
+%0A%09  %0A%09  Map%3CString,Object%3E correlationKeys = new HashMap%3CString, Object%3E();%0A%09  correlationKeys.put(%22correllationId%22, correlationKey);%0A%09  runtimeService.correlateMessage(%22serviceExecutionCompleted%22, correlationKeys);
 %0A  %7D%0A  %0A

Use correlateMessage