Skip to content

Commit

Permalink
Merge pull request #296 from NitorCreations/add-application-listener-…
Browse files Browse the repository at this point in the history
…to-netty

Add support for adding ApplicationListener when starting netty
  • Loading branch information
Edvard Fonsell committed Mar 6, 2019
2 parents ba27983 + de3aa51 commit 6ccccfa
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
- Fix workflow history cleanup to keep the actions that hold the latest values of state variables
- nFlow Explorer: Custom content to workflow definition and workflow instance pages.
- nFlow Explorer: Executors page to use standard time formatting in tooltips
- nFlow netty: Add support for registering Spring ApplicationListeners

## 5.3.3 (2019-02-04)

Expand Down
21 changes: 15 additions & 6 deletions nflow-netty/src/main/java/io/nflow/netty/StartNflow.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.SimpleCommandLinePropertySource;
import org.springframework.core.io.support.ResourcePropertySource;
Expand All @@ -35,6 +36,8 @@ public class StartNflow {

private final Set<Class<?>> annotatedContextClasses = new LinkedHashSet<>();

private final List<ApplicationListener<?>> applicationListeners = new LinkedList<>();

private final List<ResourcePropertySource> propertiesSources = new LinkedList<>();

public static void main(String[] args) throws Exception {
Expand All @@ -52,20 +55,25 @@ public static void main(String[] args) throws Exception {
new StartNflow().startNetty(argsMap);
}

public StartNflow registerSpringContext(Class<?>... springContextClass) {
annotatedContextClasses.addAll(asList(springContextClass));
public StartNflow registerSpringContext(Class<?>... springContextClasses) {
annotatedContextClasses.addAll(asList(springContextClasses));
return this;
}

public StartNflow registerSpringApplicationListener(ApplicationListener<?>... applicationListeners) {
this.applicationListeners.addAll(asList(applicationListeners));
return this;
}

public StartNflow registerSpringClasspathPropertySource(String... springPropertiesPath) throws IOException {
for (String path : springPropertiesPath) {
public StartNflow registerSpringClasspathPropertySource(String... springPropertiesPaths) throws IOException {
for (String path : springPropertiesPaths) {
propertiesSources.add(new ResourcePropertySource(path));
}
return this;
}

public StartNflow registerSpringPropertySource(ResourcePropertySource... springPropertySource) {
propertiesSources.addAll(asList(springPropertySource));
public StartNflow registerSpringPropertySource(ResourcePropertySource... springPropertySources) {
propertiesSources.addAll(asList(springPropertySources));
return this;
}

Expand All @@ -91,6 +99,7 @@ public ApplicationContext startNetty(Map<String, Object> properties) throws Exce
annotatedContextClasses.add(DelegatingWebFluxConfiguration.class);
annotatedContextClasses.add(NflowNettyConfiguration.class);
context.register(annotatedContextClasses.stream().toArray(Class<?>[]::new));
applicationListeners.forEach(applicationListener -> context.addApplicationListener(applicationListener));
context.refresh();

// Start netty
Expand Down
16 changes: 15 additions & 1 deletion nflow-netty/src/test/java/io/nflow/netty/StartNflowTest.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
package io.nflow.netty;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ApplicationContextEvent;

public class StartNflowTest {

public class TestApplicationListener implements ApplicationListener<ApplicationContextEvent> {
public ApplicationContextEvent applicationContextEvent;
@Override
public void onApplicationEvent(ApplicationContextEvent applicationContextEvent) {
this.applicationContextEvent = applicationContextEvent;
}
}

@Test
public void startNflowNetty() throws Exception {
TestApplicationListener testListener = new TestApplicationListener();
StartNflow startNflow = new StartNflow().registerSpringContext(this.getClass())
.registerSpringClasspathPropertySource("external.properties");
.registerSpringClasspathPropertySource("external.properties")
.registerSpringApplicationListener(testListener);
Map<String, Object> properties = new HashMap<>();
properties.put("nflow.db.create_on_startup", false);
properties.put("nflow.autostart", false);
properties.put("nflow.autoinit", false);
ApplicationContext ctx = startNflow.startNetty(7500, "local", "", properties);

assertNotNull(testListener.applicationContextEvent);
assertEquals("7500", ctx.getEnvironment().getProperty("port"));
assertEquals("local", ctx.getEnvironment().getProperty("env"));
assertEquals("externallyDefinedExecutorGroup", ctx.getEnvironment().getProperty("nflow.executor.group"));
Expand Down

0 comments on commit 6ccccfa

Please sign in to comment.