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 0a45415
Show file tree
Hide file tree
Showing 3 changed files with 43 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 @@ -20,10 +20,13 @@

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
Expand All @@ -37,7 +40,6 @@
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 +49,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 +124,48 @@ 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) {
this.properties = new HashMap<>();
try {
Enumeration<URL> urls = Thread.currentThread().getContextClassLoader().getResources("application.properties");
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
try (InputStream is = url.openStream()) {
Properties properties = new Properties();
properties.load(is);
properties.entrySet().forEach(e -> this.properties.put(e.getKey().toString(), e.getValue()));
} catch (IOException io) {
logger.info("Error loading properties from URL {}", url, io);
}
}
} catch (IOException io) {
logger.warn("Error searching for application.properties in classpath", io);
}
}
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 +177,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 0a45415

Please sign in to comment.