Skip to content

Commit

Permalink
[Fix #3346] Fixing mistakes
Browse files Browse the repository at this point in the history
  • Loading branch information
fjtirado committed Mar 19, 2024
1 parent 54c1594 commit 861d4ad
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public DefaultProcessEventListenerConfig(ProcessEventListener... listeners) {
}
}

public DefaultProcessEventListenerConfig(Iterable<ProcessEventListener> listeners) {
public DefaultProcessEventListenerConfig(Iterable<? extends ProcessEventListener> listeners) {
for (ProcessEventListener listener : listeners) {
register(listener);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,20 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Properties;
import java.util.ServiceLoader;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;

import org.jbpm.workflow.core.impl.WorkflowProcessImpl;
import org.jbpm.workflow.core.node.SubProcessNode;
import org.jbpm.workflow.instance.WorkflowProcessInstance;
import org.kie.api.event.process.ProcessCompletedEvent;
import org.kie.api.event.process.ProcessEventListener;
import org.kie.kogito.Addons;
import org.kie.kogito.KogitoEngine;
import org.kie.kogito.Model;
Expand All @@ -47,6 +48,7 @@
import org.kie.kogito.config.StaticConfigBean;
import org.kie.kogito.event.impl.EventFactoryUtils;
import org.kie.kogito.internal.process.event.DefaultKogitoProcessEventListener;
import org.kie.kogito.internal.process.event.KogitoProcessEventListener;
import org.kie.kogito.internal.process.runtime.KogitoWorkItemHandler;
import org.kie.kogito.process.Process;
import org.kie.kogito.process.ProcessInstance;
Expand Down Expand Up @@ -121,27 +123,41 @@ public void afterProcessCompleted(ProcessCompletedEvent event) {
}
}

private static class WorkflowApplicationBuilder {
public static class WorkflowApplicationBuilder {

private Map<String, Object> properties;
private Collection<ProcessEventListener> listeners = new ArrayList<>();
private Collection<KogitoProcessEventListener> listeners = new ArrayList<>();

private WorkflowApplicationBuilder() {
}

public WorkflowApplicationBuilder withProperties(Map<String, Object> properties) {
this.properties = properties;
return this;
}

public WorkflowApplicationBuilder withEventListener(ProcessEventListener listener, ProcessEventListener... extraListeners) {
public WorkflowApplicationBuilder withEventListener(KogitoProcessEventListener listener, KogitoProcessEventListener... extraListeners) {
listeners.add(listener);
for (ProcessEventListener extraListener : extraListeners) {
for (KogitoProcessEventListener extraListener : extraListeners) {
listeners.add(extraListener);
}
return this;
}

public StaticWorkflowApplication build() {
if (properties == null) {
Properties properties = new Properties();
try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("application.properties")) {
if (is != null) {
properties.load(is);
}
} catch (IOException io) {
logger.warn("Error loading application.properties from classpath", io);
}
this.properties = properties.entrySet().stream().collect(Collectors.toMap(e -> e.getKey().toString(), Entry::getValue));
}
Map<String, SynchronousQueue<JsonNodeModel>> queues = new ConcurrentHashMap<>();
withEventListener(new StaticCompletionEventListener(queues));
listeners.add(new StaticCompletionEventListener(queues));
StaticWorkflowApplication application = new StaticWorkflowApplication(properties, queues, listeners);
application.applicationRegisters.forEach(register -> register.register(application));
return application;
Expand All @@ -153,22 +169,14 @@ public static WorkflowApplicationBuilder builder() {
}

public static StaticWorkflowApplication create() {
Properties properties = new Properties();
try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("application.properties")) {
if (is != null) {
properties.load(is);
}
} catch (IOException io) {
logger.warn("Error loading application.properties from classpath", io);
}
return create((Map) properties);
return builder().build();
}

public static StaticWorkflowApplication create(Map<String, Object> properties) {
return builder().withProperties(properties).build();
}

private StaticWorkflowApplication(Map<String, Object> properties, Map<String, SynchronousQueue<JsonNodeModel>> queues, Collection<ProcessEventListener> listeners) {
private StaticWorkflowApplication(Map<String, Object> properties, Map<String, SynchronousQueue<JsonNodeModel>> queues, Collection<KogitoProcessEventListener> listeners) {
super(new StaticConfig(new Addons(Collections.emptySet()), new StaticProcessConfig(new CachedWorkItemHandlerConfig(),
new DefaultProcessEventListenerConfig(listeners),
new DefaultUnitOfWorkManager(new CollectingUnitOfWorkFactory())), new StaticConfigBean()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;

import org.junit.jupiter.api.Test;
import org.kie.api.event.process.ProcessCompletedEvent;
import org.kie.kogito.internal.process.event.DefaultKogitoProcessEventListener;
import org.kie.kogito.process.Process;
import org.kie.kogito.serverless.workflow.actions.SysoutAction;
import org.kie.kogito.serverless.workflow.actions.WorkflowLogLevel;
Expand Down Expand Up @@ -67,9 +70,15 @@ public class StaticFluentWorkflowApplicationTest {
@Test
void helloWorld() {
final String GREETING_STRING = "Hello World!!!";
try (StaticWorkflowApplication application = StaticWorkflowApplication.create()) {
AtomicBoolean completed = new AtomicBoolean(false);
try (StaticWorkflowApplication application = StaticWorkflowApplication.builder().withEventListener(new DefaultKogitoProcessEventListener() {
public void afterProcessCompleted(ProcessCompletedEvent event) {
completed.set(true);
}
}).build()) {
Workflow workflow = workflow("HelloWorld").start(inject(new TextNode(GREETING_STRING))).end().build();
assertThat(application.execute(workflow, Collections.emptyMap()).getWorkflowdata()).contains(new TextNode(GREETING_STRING));
assertThat(completed.get()).isTrue();
}
}

Expand Down

0 comments on commit 861d4ad

Please sign in to comment.