Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The following code shows how to start the broker by creating and opening starting a SystemConfigSystemLauncher:

Code Block
languagejava
titleStart Broker, perform messaging operations, close broker and release system resources
TaskExecutor taskExecutor = new TaskExecutorImpl();
try
{
    taskExecutor.start();
    SystemConfig systemConfig = createSystemConfig(taskExecutor, tempDir.getAbsolutePath());
    // open the systemConfig to start the broker
	systemConfig.open();
    try
    {
        // code that uses the broker
    }
    finally
    {
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.apache.qpid.server.SystemLauncher;

public class EmbeddedBroker
{
    private static final String INITIAL_CONFIGURATION = "test-initial-config.json";

    public static void main(String args[]) {
        EmbeddedBroker broker = new EmbeddedBroker();
        try {
            broker.start();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            systemConfigSystem.closeexit(1);
        }
    }
finally
{
    private void taskExecutor.stopstart();
}

As can be seen the main task is creating the SystemConfig which is shown in the following snippet:

Code Block
languagejava
titleBroker startup code
private static final String INITIAL_CONFIGURATION = "test-initial-config.json";
private static final Principal SYSTEM_PRINCIPAL = () -> "system";

private static SystemConfig createSystemConfig(final TaskExecutor taskExecutor, String workDirectory) throws IOException
{
    // create event logger
    EventLogger eventLogger = new EventLogger(new LoggingMessageLogger());

    // get memory system config factory
    PluggableFactoryLoader<SystemConfigFactory> configFactoryLoader = new PluggableFactoryLoader<>(SystemConfigFactory.class);
    SystemConfigFactory configFactory = configFactoryLoader.get(MemorySystemConfigImpl.SYSTEM_CONFIG_TYPE);

    // system config attributes defining initial configuration location
 throws Exception {
        final SystemLauncher systemLauncher = new SystemLauncher();
        try {
            systemLauncher.startup(createSystemConfig());
            // performMessagingOperations();
        } finally {
            systemLauncher.shutdown();
        }
    }

    private Map<String, Object> createSystemConfig() {
        Map<String, Object> attributes = new HashMap<>();
        URL initialConfig = TestEmbeddedBroker.class.getClassLoader().getResource(INITIAL_CONFIGURATION);
        attributes.put("type", "Memory");
        attributes.put("initialConfigurationLocation", initialConfig.toExternalForm());
        attributes.put("startupLoggedToSystemOut", falsetrue);

	//  create system config
    return configFactory.newInstance(taskExecutor, eventLogger, SYSTEM_PRINCIPAL, attributes);attributes;
    }
}

Final Remarks

Info
titleNote

Please note, that provided code samples do not start broker with http management. If http management is required, the http port and http-management plugin need to be added into initial configuration. A maven dependency for qpid-broker-plugins-management-http needs to be added into project as well.