Created
August 16, 2014 00:44
-
-
Save jesuino/a6c86560a6a9a510724f to your computer and use it in GitHub Desktop.
Trying to handle a task usin JMS and REST
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.redhat.gss.bpms.client; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.Properties; | |
import javax.naming.InitialContext; | |
import javax.naming.NamingException; | |
import org.kie.api.runtime.KieSession; | |
import org.kie.api.runtime.manager.RuntimeEngine; | |
import org.kie.api.runtime.process.ProcessInstance; | |
import org.kie.api.task.TaskService; | |
import org.kie.api.task.model.Status; | |
import org.kie.api.task.model.TaskSummary; | |
import org.kie.services.client.api.RemoteJmsRuntimeEngineFactory; | |
import org.kie.services.client.api.RemoteRestRuntimeFactory; | |
public class Main { | |
static final String DEPLOYMENT_ID = "org.jugvale.tdc:tdc-ola:1.1"; | |
static final InitialContext CONTEXT = createInitialContext(); | |
static final String PROCESS_NAME = "tdc-ola.ola-mundo-tdc"; | |
static final String USERNAME = "jesuino"; | |
static final String PASSWORD = "redhat2014!"; | |
static final String JMS_URL = "remote://localhost:4447"; | |
static final String HTTP_URL = "http://localhost:8080/business-central"; | |
private static final int MAX_WAIT = 100000; | |
private static final List<Status> ALL_TASK_STATUS = Arrays.asList(Status | |
.values()); | |
public static void main(String[] args) throws MalformedURLException, | |
InterruptedException { | |
tasksHandlingTest(); | |
} | |
private static void tasksHandlingTest() throws MalformedURLException, | |
InterruptedException { | |
RuntimeEngine engine = restEngine(); | |
KieSession ksession = engine.getKieSession(); | |
ProcessInstance pInstance = ksession.startProcess(PROCESS_NAME, null); | |
System.out.println("Process: " + pInstance.getId()); | |
TaskService taskService = engine.getTaskService(); | |
taskService.getTasksAssignedAsPotentialOwner(USERNAME, "en-UK") | |
.forEach( | |
t -> System.out.printf("%s: %d - %s - %s\n", | |
t.getProcessId(), t.getId(), t.getName(), | |
t.getStatus())); | |
List<TaskSummary> pTasks = taskService | |
.getTasksByStatusByProcessInstanceId(pInstance.getId(), | |
ALL_TASK_STATUS, "en-UK"); | |
TaskSummary t = pTasks.get(0); | |
Status status = t.getStatus(); | |
Long tId = t.getId(); | |
while (status != Status.Completed) { | |
System.out.println("Status: " + status); | |
switch (status) { | |
case Reserved: | |
System.out.println("Starting task " + tId); | |
new Thread().start(); | |
go(() -> taskService.start(tId, USERNAME)); | |
break; | |
case InProgress: | |
System.out.println("Completing task " + tId); | |
go(() -> taskService.complete(tId, USERNAME, null)); | |
break; | |
case Ready: | |
System.out.println("Claiming task " + tId); | |
go(() -> taskService.claim(tId, USERNAME)); | |
break; | |
default: | |
System.out.println("Break on status " + t.getStatus()); | |
break; | |
} | |
System.out.println("Sleeing for 5 seconds"); | |
Thread.sleep(5000); | |
} | |
System.out.println("Task handled..."); | |
} | |
static private void go(Runnable r) { | |
// perform in another thread | |
new Thread(r).start(); | |
} | |
static private RuntimeEngine jmsEngine() { | |
return new RemoteJmsRuntimeEngineFactory(DEPLOYMENT_ID, CONTEXT, | |
USERNAME, PASSWORD, MAX_WAIT).newRuntimeEngine(); | |
} | |
static private RuntimeEngine restEngine() throws MalformedURLException { | |
return new RemoteRestRuntimeFactory(DEPLOYMENT_ID, new URL(HTTP_URL), | |
USERNAME, PASSWORD).newRuntimeEngine(); | |
} | |
static private InitialContext createInitialContext() { | |
Properties props = new Properties(); | |
props.setProperty(InitialContext.INITIAL_CONTEXT_FACTORY, | |
"org.jboss.naming.remote.client.InitialContextFactory"); | |
props.setProperty(InitialContext.PROVIDER_URL, JMS_URL); | |
props.setProperty(InitialContext.SECURITY_PRINCIPAL, USERNAME); | |
props.setProperty(InitialContext.SECURITY_CREDENTIALS, PASSWORD); | |
try { | |
return new InitialContext(props); | |
} catch (NamingException e) { | |
throw new Error(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment