diff --git a/broker/artemis/plugin/src/main/java/org/eclipse/kapua/broker/artemis/plugin/security/ActivateCallback.java b/broker/artemis/plugin/src/main/java/org/eclipse/kapua/broker/artemis/plugin/security/ActivateCallback.java new file mode 100644 index 00000000000..fbf27fd7c4a --- /dev/null +++ b/broker/artemis/plugin/src/main/java/org/eclipse/kapua/broker/artemis/plugin/security/ActivateCallback.java @@ -0,0 +1,49 @@ +/******************************************************************************* + * Copyright (c) 2022 Eurotech and/or its affiliates and others + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Eurotech + *******************************************************************************/ +package org.eclipse.kapua.broker.artemis.plugin.security; + +import org.eclipse.kapua.broker.artemis.plugin.security.connector.AcceptorHandler; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Class to intercept Broker lifecycle events + */ +public class ActivateCallback implements org.apache.activemq.artemis.core.server.ActivateCallback { + + private final Logger logger = LoggerFactory.getLogger(ActivateCallback.class); + + private final AcceptorHandler acceptorHandler; + + public ActivateCallback(AcceptorHandler acceptorHandler) { + this.acceptorHandler = acceptorHandler; + } + + @Override + /** + * Use this callback to start Acceptors for now (but could be used to do whatever needs to have a fully running broker + */ + public void activationComplete() { + logger.info("Broker activation completed!"); + org.apache.activemq.artemis.core.server.ActivateCallback.super.activationComplete(); + logger.info("Creating acceptors..."); + try { + acceptorHandler.syncAcceptors(); + } catch (Exception e) { + logger.error("Creating acceptors... ERROR: {}", e.getMessage(), e); + //TODO throw runtime? the broker doesn't work properly if not all the acceptors are created + } + logger.info("Creating acceptors... DONE"); + } + +} diff --git a/broker/artemis/plugin/src/main/java/org/eclipse/kapua/broker/artemis/plugin/security/ServerPlugin.java b/broker/artemis/plugin/src/main/java/org/eclipse/kapua/broker/artemis/plugin/security/ServerPlugin.java index 36dffa859db..47cd62cd7ce 100644 --- a/broker/artemis/plugin/src/main/java/org/eclipse/kapua/broker/artemis/plugin/security/ServerPlugin.java +++ b/broker/artemis/plugin/src/main/java/org/eclipse/kapua/broker/artemis/plugin/security/ServerPlugin.java @@ -137,8 +137,8 @@ public void registered(ActiveMQServer server) { serverContext.init(server); acceptorHandler = new AcceptorHandler(server, brokerSetting.getMap(String.class, BrokerSettingKey.ACCEPTORS)); - //init acceptors - acceptorHandler.syncAcceptors(); + //init activateCallback to handle acceptor initialization instead of calling it from here + server.registerActivateCallback(new ActivateCallback(acceptorHandler)); deviceConnectionEventListenerService.addReceiver(serviceEvent -> processDeviceConnectionEvent(serviceEvent)); diff --git a/broker/artemis/plugin/src/main/java/org/eclipse/kapua/broker/artemis/plugin/security/connector/AcceptorHandler.java b/broker/artemis/plugin/src/main/java/org/eclipse/kapua/broker/artemis/plugin/security/connector/AcceptorHandler.java index aaae59ab96e..af887b68cd9 100644 --- a/broker/artemis/plugin/src/main/java/org/eclipse/kapua/broker/artemis/plugin/security/connector/AcceptorHandler.java +++ b/broker/artemis/plugin/src/main/java/org/eclipse/kapua/broker/artemis/plugin/security/connector/AcceptorHandler.java @@ -83,32 +83,29 @@ public String removeAcceptor(String name) throws Exception { * @throws Exception */ public void syncAcceptors() throws Exception { - logger.info("Init acceptors... server started: {} - {}", server.isStarted(), server.getState()); - if (server.isStarted()) { - List acceptorToRemove = new ArrayList<>(); - server.getConfiguration().getAcceptorConfigurations().forEach(tc -> { - String acceptorName = tc.getName(); - logger.info("Checking acceptor {}", acceptorName); - if (definedAcceptors.get(acceptorName) == null) { - acceptorToRemove.add(acceptorName); - logger.info("Adding acceptor {} to the remove list", acceptorName); - } else { - logger.info("Leaving acceptor {} running", acceptorName); - } - }); - acceptorToRemove.forEach(acceptorName -> { - logger.info("Stopping acceptor {}...", acceptorName); - try { - server.getRemotingService().getAcceptor(acceptorName).stop(); - server.getRemotingService().destroyAcceptor(acceptorName); - TransportConfiguration tc = getByName(acceptorName); - server.getConfiguration().getAcceptorConfigurations().remove(tc); - } catch (Exception e) { - logger.error("Error stopping acceptor {}... Error: {}", acceptorName, e.getMessage(), e); - } - logger.info("Stopping acceptor {}... DONE", acceptorName); - }); - } + List acceptorToRemove = new ArrayList<>(); + server.getConfiguration().getAcceptorConfigurations().forEach(tc -> { + String acceptorName = tc.getName(); + logger.info("Checking acceptor {}", acceptorName); + if (definedAcceptors.get(acceptorName) == null) { + acceptorToRemove.add(acceptorName); + logger.info("Adding acceptor {} to the remove list", acceptorName); + } else { + logger.info("Leaving acceptor {} running", acceptorName); + } + }); + acceptorToRemove.forEach(acceptorName -> { + logger.info("Stopping acceptor {}...", acceptorName); + try { + server.getRemotingService().getAcceptor(acceptorName).stop(); + server.getRemotingService().destroyAcceptor(acceptorName); + TransportConfiguration tc = getByName(acceptorName); + server.getConfiguration().getAcceptorConfigurations().remove(tc); + } catch (Exception e) { + logger.error("Error stopping acceptor {}... Error: {}", acceptorName, e.getMessage(), e); + } + logger.info("Stopping acceptor {}... DONE", acceptorName); + }); // server.getConfiguration().clearAcceptorConfigurations(); definedAcceptors.forEach((name, uri) -> {