public void executeWorkItem(WorkItem workItem, final WorkItemManager manager) { String implementation = (String) workItem.getParameter("implementation"); if ("##WebService".equalsIgnoreCase(implementation)) { String interfaceRef = (String) workItem.getParameter("interfaceImplementationRef"); String operationRef = (String) workItem.getParameter("operationImplementationRef"); Object parameter = workItem.getParameter("Parameter"); WSMode mode = WSMode.valueOf( workItem.getParameter("mode") == null ? "SYNC" : ((String) workItem.getParameter("mode")).toUpperCase()); try { Client client = getWSClient(workItem, interfaceRef); if (client == null) { throw new IllegalStateException( "Unable to create client for web service " + interfaceRef + " - " + operationRef); } switch (mode) { case SYNC: Object[] result = client.invoke(operationRef, parameter); Map<String, Object> output = new HashMap<String, Object>(); if (result == null || result.length == 0) { output.put("Result", null); } else { output.put("Result", result[0]); } manager.completeWorkItem(workItem.getId(), output); break; case ASYNC: final ClientCallback callback = new ClientCallback(); final long workItemId = workItem.getId(); final String deploymentId = nonNull(((WorkItemImpl) workItem).getDeploymentId()); final long processInstanceId = workItem.getProcessInstanceId(); client.invoke(callback, operationRef, parameter); new Thread( new Runnable() { public void run() { try { Object[] result = callback.get(asyncTimeout, TimeUnit.SECONDS); Map<String, Object> output = new HashMap<String, Object>(); if (callback.isDone()) { if (result == null) { output.put("Result", null); } else { output.put("Result", result[0]); } } RuntimeManager manager = RuntimeManagerRegistry.get().getManager(deploymentId); if (manager != null) { RuntimeEngine engine = manager.getRuntimeEngine( ProcessInstanceIdContext.get(processInstanceId)); engine .getKieSession() .getWorkItemManager() .completeWorkItem(workItemId, output); manager.disposeRuntimeEngine(engine); } else { // in case there is no RuntimeManager available use available ksession, // as it might be used without runtime manager at all ksession.getWorkItemManager().completeWorkItem(workItemId, output); } } catch (Exception e) { logger.error( "Error encountered while invoking ws operation asynchronously ", e); } } }) .start(); break; case ONEWAY: ClientCallback callbackFF = new ClientCallback(); client.invoke(callbackFF, operationRef, parameter); manager.completeWorkItem(workItem.getId(), new HashMap<String, Object>()); break; default: break; } } catch (Exception e) { handleException(e, interfaceRef, operationRef, parameter.getClass().getName(), parameter); } } else { executeJavaWorkItem(workItem, manager); } }
public void executeWorkItem(WorkItem workItem, final WorkItemManager manager) { Object[] parameters = null; String interfaceRef = (String) workItem.getParameter("Interface"); String operationRef = (String) workItem.getParameter("Operation"); String endpointAddress = (String) workItem.getParameter("Endpoint"); if (workItem.getParameter("Parameter") instanceof Object[]) { parameters = (Object[]) workItem.getParameter("Parameter"); } else if (workItem.getParameter("Parameter") != null && workItem.getParameter("Parameter").getClass().isArray()) { int length = Array.getLength(workItem.getParameter("Parameter")); parameters = new Object[length]; for (int i = 0; i < length; i++) { parameters[i] = Array.get(workItem.getParameter("Parameter"), i); } } else { parameters = new Object[] {workItem.getParameter("Parameter")}; } String modeParam = (String) workItem.getParameter("Mode"); WSMode mode = WSMode.valueOf(modeParam == null ? "SYNC" : modeParam.toUpperCase()); try { Client client = getWSClient(workItem, interfaceRef); if (client == null) { throw new IllegalStateException( "Unable to create client for web service " + interfaceRef + " - " + operationRef); } // Override endpoint address if configured. if (endpointAddress != null && !"".equals(endpointAddress)) { client.getRequestContext().put(Message.ENDPOINT_ADDRESS, endpointAddress); } switch (mode) { case SYNC: Object[] result = client.invoke(operationRef, parameters); Map<String, Object> output = new HashMap<String, Object>(); if (result == null || result.length == 0) { output.put("Result", null); } else { output.put("Result", result[0]); } logger.debug( "Received sync response {} completeing work item {}", result, workItem.getId()); manager.completeWorkItem(workItem.getId(), output); break; case ASYNC: final ClientCallback callback = new ClientCallback(); final long workItemId = workItem.getId(); final String deploymentId = nonNull(((WorkItemImpl) workItem).getDeploymentId()); final long processInstanceId = workItem.getProcessInstanceId(); client.invoke(callback, operationRef, parameters); new Thread( new Runnable() { public void run() { try { Object[] result = callback.get(asyncTimeout, TimeUnit.SECONDS); Map<String, Object> output = new HashMap<String, Object>(); if (callback.isDone()) { if (result == null) { output.put("Result", null); } else { output.put("Result", result[0]); } } logger.debug( "Received async response {} completeing work item {}", result, workItemId); RuntimeManager manager = RuntimeManagerRegistry.get().getManager(deploymentId); if (manager != null) { RuntimeEngine engine = manager.getRuntimeEngine( ProcessInstanceIdContext.get(processInstanceId)); engine .getKieSession() .getWorkItemManager() .completeWorkItem(workItemId, output); manager.disposeRuntimeEngine(engine); } else { // in case there is no RuntimeManager available use available ksession, // as it might be used without runtime manager at all ksession.getWorkItemManager().completeWorkItem(workItemId, output); } } catch (Exception e) { e.printStackTrace(); throw new RuntimeException( "Error encountered while invoking ws operation asynchronously", e); } } }) .start(); break; case ONEWAY: ClientCallback callbackFF = new ClientCallback(); client.invoke(callbackFF, operationRef, parameters); logger.debug( "One way operation, not going to wait for response, completing work item {}", workItem.getId()); manager.completeWorkItem(workItem.getId(), new HashMap<String, Object>()); break; default: break; } } catch (Exception e) { handleException(e); } }
public void executeWorkItem(WorkItem workItem, final WorkItemManager manager) { Object[] parameters = null; String interfaceRef = (String) workItem.getParameter("Interface"); String operationRef = (String) workItem.getParameter("Operation"); if (workItem.getParameter("Parameter") instanceof Object[]) { parameters = (Object[]) workItem.getParameter("Parameter"); } else if (workItem.getParameter("Parameter") != null && workItem.getParameter("Parameter").getClass().isArray()) { int length = Array.getLength(workItem.getParameter("Parameter")); parameters = new Object[length]; for (int i = 0; i < length; i++) { parameters[i] = Array.get(workItem.getParameter("Parameter"), i); } } else { parameters = new Object[] {workItem.getParameter("Parameter")}; } String modeParam = (String) workItem.getParameter("Mode"); WSMode mode = WSMode.valueOf(modeParam == null ? "SYNC" : modeParam.toUpperCase()); try { Client client = getWSClient(workItem, interfaceRef); if (client == null) { throw new IllegalStateException( "Unable to create client for web service " + interfaceRef + " - " + operationRef); } switch (mode) { case SYNC: Object[] result = client.invoke(operationRef, parameters); Map<String, Object> output = new HashMap<String, Object>(); if (result == null || result.length == 0) { output.put("Result", null); } else { output.put("Result", result[0]); } logger.debug( "Received sync response {} completeing work item {}", result, workItem.getId()); manager.completeWorkItem(workItem.getId(), output); break; case ASYNC: final ClientCallback callback = new ClientCallback(); final long workItemId = workItem.getId(); client.invoke(callback, operationRef, parameters); new Thread( new Runnable() { public void run() { try { Object[] result = callback.get(asyncTimeout, TimeUnit.SECONDS); Map<String, Object> output = new HashMap<String, Object>(); if (callback.isDone()) { if (result == null) { output.put("Result", null); } else { output.put("Result", result[0]); } } logger.debug( "Received async response {} completeing work item {}", result, workItemId); ksession.getWorkItemManager().completeWorkItem(workItemId, output); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException( "Error encountered while invoking ws operation asynchronously", e); } } }) .start(); break; case ONEWAY: ClientCallback callbackFF = new ClientCallback(); client.invoke(callbackFF, operationRef, parameters); logger.debug( "One way operation, not going to wait for response, completing work item {}", workItem.getId()); manager.completeWorkItem(workItem.getId(), new HashMap<String, Object>()); break; default: break; } } catch (Exception e) { handleException(e); } }