public static void main(String[] args) throws IOException { final Server server = new Server(); server.startServer(); System.out.println("Server started, version 0.12.1-SNAPSHOT"); //Bind a shutdown hook Runtime.getRuntime().addShutdownHook(new Thread(server::stopServer)); }
final IConfig classPathConfig = new ResourceLoaderConfig(classpathLoader); final Server mqttBroker = new Server(); List<? extends InterceptHandler> userHandlers = Collections.singletonList(new PublisherListener()); mqttBroker.startServer(classPathConfig, userHandlers); mqttBroker.stopServer(); System.out.println("Broker stopped"); })); .build(); mqttBroker.internalPublish(message, "INTRLPUB"); System.out.println("After self publish");
/** * Starts Moquette bringing the configuration from the file located at m_config/moquette.conf * * @throws IOException in case of any IO error. */ public void startServer() throws IOException { File defaultConfigurationFile = defaultConfigFile(); LOG.info("Starting Moquette integration. Configuration file path={}", defaultConfigurationFile.getAbsolutePath()); IResourceLoader filesystemLoader = new FileResourceLoader(defaultConfigurationFile); final IConfig config = new ResourceLoaderConfig(filesystemLoader); startServer(config); }
@Test public void checkDontPublishInactiveClientsAfterServerRestart() throws Exception { IMqttClient conn = subscribeAndPublish("/topic"); conn.disconnect(); // shutdown the integration m_server.stopServer(); // restart the integration m_server.startServer(IntegrationUtils.prepareTestProperties()); m_publisher.connect(); m_publisher.publish("/topic", "Hello world MQTT!!".getBytes(UTF_8), 0, false); }
private void stopServer() { m_server.stopServer(); }
/** * Starts Moquette with config provided by an implementation of IConfig class and with the set * of InterceptHandler. * * @param config the configuration to use to start the broker. * @param handlers the handlers to install in the broker. * @throws IOException in case of any IO Error. */ public void startServer(IConfig config, List<? extends InterceptHandler> handlers) throws IOException { LOG.debug("Starting moquette integration using IConfig instance and intercept handlers"); startServer(config, handlers, null, null, null); }
initInterceptors(config, handlers); LOG.debug("Initialized MQTT protocol processor"); if (sslCtxCreator == null) { sslCtxCreator = new DefaultMoquetteSslContextCreator(config); authenticator = initializeAuthenticator(authenticator, config); authorizatorPolicy = initializeAuthorizatorPolicy(authorizatorPolicy, config);
.build(); m_server.internalPublish(message, "INTRLPUB"); m_server.internalPublish(message, "INTRLPUB");
private void initInterceptors(IConfig props, List<? extends InterceptHandler> embeddedObservers) { LOG.info("Configuring message interceptors..."); List<InterceptHandler> observers = new ArrayList<>(embeddedObservers); String interceptorClassName = props.getProperty(BrokerConstants.INTERCEPT_HANDLER_PROPERTY_NAME); if (interceptorClassName != null && !interceptorClassName.isEmpty()) { InterceptHandler handler = loadClass(interceptorClassName, InterceptHandler.class, io.moquette.broker.Server.class, this); if (handler != null) { observers.add(handler); } } interceptor = new BrokerInterceptor(props, observers); }
@Ignore("This test hasn't any meaning using in memory storage service") @Test public void testCleanSession_maintainClientSubscriptions_withServerRestart() throws Exception { LOG.info("*** testCleanSession_maintainClientSubscriptions_withServerRestart ***"); MqttConnectOptions options = new MqttConnectOptions(); options.setCleanSession(false); m_client.connect(options); m_client.subscribe("/topic", 0); m_client.disconnect(); m_server.stopServer(); m_server.startServer(IntegrationUtils.prepareTestProperties()); // reconnect and publish m_client.connect(options); m_client.publish("/topic", "Test my payload".getBytes(UTF_8), 0, false); assertEquals("/topic", m_messagesCollector.getTopic()); }
private void stopServer() { m_server.stopServer(); }
/** * Starts Moquette bringing the configuration files from the given Config implementation. * * @param config the configuration to use to start the broker. * @throws IOException in case of any IO Error. */ public void startServer(IConfig config) throws IOException { LOG.debug("Starting Moquette integration using IConfig instance"); startServer(config, null); }
private IAuthenticator initializeAuthenticator(IAuthenticator authenticator, IConfig props) { // if (authenticator == null) { // authenticator = new AcceptAllAuthenticator(); // } // return authenticator; LOG.debug("Configuring MQTT authenticator"); String authenticatorClassName = props.getProperty(BrokerConstants.AUTHENTICATOR_CLASS_NAME, ""); if (authenticator == null && !authenticatorClassName.isEmpty()) { authenticator = loadClass(authenticatorClassName, IAuthenticator.class, IConfig.class, props); } IResourceLoader resourceLoader = props.getResourceLoader(); if (authenticator == null) { String passwdPath = props.getProperty(BrokerConstants.PASSWORD_FILE_PROPERTY_NAME, ""); if (passwdPath.isEmpty()) { authenticator = new AcceptAllAuthenticator(); } else { authenticator = new ResourceAuthenticator(resourceLoader, passwdPath); } LOG.info("An {} authenticator instance will be used", authenticator.getClass().getName()); } return authenticator; }
protected void startServer() throws IOException { m_server = new Server(); m_server.startServer(); }
@Test public void checkRestartCleanSubscriptionTree() throws Exception { // subscribe to /topic m_subscriber.connect(CLEAN_SESSION_OPT); m_subscriber.subscribe("/topic", 1); m_subscriber.disconnect(); // shutdown the integration m_server.stopServer(); // restart the integration m_server.startServer(IntegrationUtils.prepareTestProperties()); // reconnect the Subscriber subscribing to the same /topic but different QoS m_subscriber.connect(CLEAN_SESSION_OPT); m_subscriber.subscribe("/topic", 2); // should be just one registration so a publisher receive one notification m_publisher.connect(CLEAN_SESSION_OPT); m_publisher.publish("/topic", "Hello world MQTT!!".getBytes(UTF_8), 1, false); // read the messages MqttMessage msg = m_messageCollector.waitMessage(1); assertEquals("Hello world MQTT!!", new String(msg.getPayload(), UTF_8)); // no more messages on the same topic will be received assertNull(m_messageCollector.waitMessage(1)); }
private void stopServer() { m_server.stopServer(); }
/** * Starts the integration with the given properties. * <p> * Its suggested to at least have the following properties: * <ul> * <li>port</li> * <li>password_file</li> * </ul> * * @param configProps the properties map to use as configuration. * @throws IOException in case of any IO Error. */ public void startServer(Properties configProps) throws IOException { LOG.debug("Starting Moquette integration using properties object"); final IConfig config = new MemoryConfig(configProps); startServer(config); }
String authorizatorClassName = props.getProperty(BrokerConstants.AUTHORIZATOR_CLASS_NAME, ""); if (authorizatorPolicy == null && !authorizatorClassName.isEmpty()) { authorizatorPolicy = loadClass(authorizatorClassName, IAuthorizatorPolicy.class, IConfig.class, props);
protected void startServer(Properties props) throws IOException { m_server = new Server(); m_config = new MemoryConfig(props); m_server.startServer(m_config); }
@Test public void testClientDoesntRemainSubscribedAfterASubscriptionAndServerRestart() throws Exception { // subscribe to /topic m_subscriber.connect(); // subscribe /topic m_subscriber.subscribe("/topic", 0); // unsubscribe from /topic m_subscriber.unsubscribe("/topic"); m_subscriber.disconnect(); // shutdown the integration m_server.stopServer(); // restart the integration m_server.startServer(IntegrationUtils.prepareTestProperties()); // subscriber reconnects m_subscriber = new MqttClient("tcp://localhost:1883", "Subscriber", s_dataStore); m_subscriber.setCallback(m_messageCollector); m_subscriber.connect(); // publisher publishes on /topic m_publisher = new MqttClient("tcp://localhost:1883", "Publisher", s_pubDataStore); m_publisher.connect(); m_publisher.publish("/topic", "Hello world MQTT!!".getBytes(UTF_8), 1, false); // Expected // the subscriber doesn't get notified (it's fully unsubscribed) assertNull(m_messageCollector.waitMessage(1)); }