Skip to content

Commit

Permalink
concord-server: auto-wire modules in dist instead of impl
Browse files Browse the repository at this point in the history
Gives the option of excluding sisu from the classpath if auto-wiring is
not required.
  • Loading branch information
ibodrov committed Nov 16, 2023
1 parent 43f3c52 commit 6436ad0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down Expand Up @@ -40,10 +40,11 @@

public class TestingConcordServer implements AutoCloseable {

private final Map<String, String> extraConfiguration;
private final List<Function<Config, Module>> extraModules;

private PostgreSQLContainer<?> db;
private ConcordServer server;
private Map<String, String> extraConfiguration;
private List<Function<Config, Module>> extraModules;

public TestingConcordServer(Map<String, String> extraConfiguration, List<Function<Config, Module>> extraModules) {
this.extraConfiguration = requireNonNull(extraConfiguration);
Expand Down Expand Up @@ -119,10 +120,24 @@ private static String randomString() {
return Base64.getEncoder().encodeToString(ab);
}

/**
* Just an example.
*/
public static void main(String[] args) throws Exception {
try (TestingConcordServer server = new TestingConcordServer(Map.of("process.watchdogPeriod", "10 seconds"), List.of())) {
server.start();
Thread.sleep(100000);

System.out.println("""
==============================================================
UI: http://localhost:8001/
DB:
JDBC URL: %s
username: %s
password: %s
""".formatted(server.getDb().getJdbcUrl(), server.getDb().getUsername(), server.getDb().getPassword()));

Thread.currentThread().join();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@

import com.walmartlabs.concord.server.ConcordServer;
import com.walmartlabs.concord.server.Version;
import org.eclipse.sisu.space.BeanScanning;
import org.eclipse.sisu.space.SpaceModule;
import org.eclipse.sisu.space.URLClassSpace;
import org.eclipse.sisu.wire.WireModule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.bridge.SLF4JBridgeHandler;
Expand All @@ -38,10 +42,15 @@ public static void main(String[] args) throws Exception {

long t1 = System.currentTimeMillis();

ConcordServer.withAutoWiring()
.start();
autoWire().start();

long t2 = System.currentTimeMillis();
log.info("main -> started in {}ms", (t2 - t1));
}

public static ConcordServer autoWire() throws Exception {
// works as a plugin system by automatically wiring all @Named modules and beans in the classpath
ClassLoader cl = ConcordServer.class.getClassLoader();
return ConcordServer.withModules(new WireModule(new SpaceModule(new URLClassSpace(cl), BeanScanning.GLOBAL_INDEX)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@
import com.google.inject.Module;
import com.walmartlabs.concord.server.boot.BackgroundTasks;
import com.walmartlabs.concord.server.boot.HttpServer;
import org.eclipse.sisu.space.BeanScanning;
import org.eclipse.sisu.space.SpaceModule;
import org.eclipse.sisu.space.URLClassSpace;
import org.eclipse.sisu.wire.WireModule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -51,16 +47,6 @@ public final class ConcordServer {

private final Object controlMutex = new Object();

/**
* Start ConcordServer by scanning the local class path for the implementations of
* {@link HttpServer} or {@link BackgroundTasks}.
*/
public static ConcordServer withAutoWiring() throws Exception {
ClassLoader cl = ConcordServer.class.getClassLoader();
return withModules(
new WireModule(new SpaceModule(new URLClassSpace(cl), BeanScanning.GLOBAL_INDEX)));
}

public static ConcordServer withModules(Module... modules) throws Exception {
return withModules(List.of(modules));
}
Expand Down

0 comments on commit 6436ad0

Please sign in to comment.