[ 
https://issues.apache.org/jira/browse/ARTEMIS-4230?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17723323#comment-17723323
 ] 

Justin Bertram commented on ARTEMIS-4230:
-----------------------------------------

You can already do what you want, but {{EmbeddedActiveMQ}} doesn't quite give 
you enough control. You can use code like this instead:

{code:java}
...
import java.util.concurrent.atomic.AtomicReference;

import org.apache.activemq.artemis.core.server.ActiveMQServer;
import org.apache.activemq.artemis.core.server.ActiveMQServers;
...
ActiveMQServer server = ActiveMQServers.newActiveMQServer(fileConfig, null, 
null);
final AtomicReference<Exception> activationFailure = new AtomicReference<>();
server.registerActivationFailureListener(exception -> 
activationFailure.set(exception));
server.start();
if (activationFailure.get() != null) {
   server.stop();
   return "Problem during broker startup: " + 
activationFailure.get().getMessage();
}
return server.getStatus();{code}

> Get errors on startup of broker
> -------------------------------
>
>                 Key: ARTEMIS-4230
>                 URL: https://issues.apache.org/jira/browse/ARTEMIS-4230
>             Project: ActiveMQ Artemis
>          Issue Type: Improvement
>    Affects Versions: 2.28.0
>            Reporter: Raymond
>            Assignee: Justin Bertram
>            Priority: Minor
>
> I start an embedded Artemis broker like this:
>  
> {code:java}
> try {
>     broker = new EmbeddedActiveMQ();
>     broker.setConfigResourcePath(fileConfig);
>     broker.start();
>     return broker.getActiveMQServer().getStatus();
> } catch (Throwable e) {
>     return "Failed to start broker. Reason: " + e.getMessage();
> }
> {code}
> When I started the broker and port 5672 is already binded, I get the 
> following error in the logs:
> {code:java}
> 2023-04-01 01:40:35.897 ERROR 18340 --- [  restartedMain] 
> org.apache.activemq.artemis.core.server  : AMQ224104: Error starting the 
> Acceptor amqp {scheme=tcp, tcpReceiveBufferSize=1048576, port=5672, 
> host=0.0.0.0, protocols=AMQP,CORE, useEpoll=true, tcpSendBufferSize=1048576}
> 2023-04-01 01:40:35.901 ERROR 18340 --- [  restartedMain] 
> org.apache.activemq.artemis.core.server  : AMQ224000: Failure in 
> initialisationjava.lang.IllegalStateException: AMQ229230: Failed to bind 
> acceptor amqp to 0.0.0.0:5672
>         at 
> org.apache.activemq.artemis.core.remoting.impl.netty.NettyAcceptor.startServerChannels(NettyAcceptor.java:736)
>         at 
> org.apache.activemq.artemis.core.remoting.impl.netty.NettyAcceptor.start(NettyAcceptor.java:532)
>         at 
> org.apache.activemq.artemis.core.remoting.server.impl.RemotingServiceImpl.startAcceptors(RemotingServiceImpl.java:315)
>         at 
> org.apache.activemq.artemis.core.server.impl.ActiveMQServerImpl.completeActivation(ActiveMQServerImpl.java:3450)
>         at 
> org.apache.activemq.artemis.core.server.impl.LiveOnlyActivation.run(LiveOnlyActivation.java:80)
>         at 
> org.apache.activemq.artemis.core.server.impl.ActiveMQServerImpl.internalStart(ActiveMQServerImpl.java:684)
>         at 
> org.apache.activemq.artemis.core.server.impl.ActiveMQServerImpl.start(ActiveMQServerImpl.java:581)
>         at 
> org.apache.activemq.artemis.core.server.embedded.EmbeddedActiveMQ.start(EmbeddedActiveMQ.java:134)
>         at 
> org.assimbly.broker.impl.ActiveMQArtemis.start(ActiveMQArtemis.java:91)
>         at 
> org.assimbly.brokerrest.ManagedBrokerRuntime.start(ManagedBrokerRuntime.java:51)
>         at 
> org.assimbly.gateway.web.rest.broker.BrokerResource.init(BrokerResource.java:142)
>         at 
> java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>         at 
> java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
>         at 
> java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>         at java.base/java.lang.reflect.Method.invoke(Method.java:566)
>         at 
> org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:389)
>         at 
> org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:333)
>         at 
> org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:157)
>         at 
> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:440)
>         at 
> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1796)
>         at 
> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620)
>         at 
> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
>         at 
> org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
>         at 
> org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
>         at 
> org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
>         at 
> org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
>         at 
> org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:955)
>         at 
> org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:920)
>         at 
> org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
>         at 
> org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147)
>         at 
> org.springframework.boot.SpringApplication.refresh(SpringApplication.java:731)
>         at 
> org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408)
>         at 
> org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
>         at org.assimbly.gateway.GatewayApp.main(GatewayApp.java:86)
>         at 
> java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>         at 
> java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
>         at 
> java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>         at java.base/java.lang.reflect.Method.invoke(Method.java:566)
>         at 
> org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
> Caused by: java.net.BindException: Address already in use: bind
>         at java.base/sun.nio.ch.Net.bind0(Native Method)
>         at java.base/sun.nio.ch.Net.bind(Net.java:459)
>         at java.base/sun.nio.ch.Net.bind(Net.java:448)
>         at 
> java.base/sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:227)
>         at 
> io.netty.channel.socket.nio.NioServerSocketChannel.doBind(NioServerSocketChannel.java:141)
>         at 
> io.netty.channel.AbstractChannel$AbstractUnsafe.bind(AbstractChannel.java:562)
>         at 
> io.netty.channel.DefaultChannelPipeline$HeadContext.bind(DefaultChannelPipeline.java:1334)
>         at 
> io.netty.channel.AbstractChannelHandlerContext.invokeBind(AbstractChannelHandlerContext.java:600)
>         at 
> io.netty.channel.AbstractChannelHandlerContext.bind(AbstractChannelHandlerContext.java:579)
>         at 
> io.netty.channel.DefaultChannelPipeline.bind(DefaultChannelPipeline.java:973)
>         at io.netty.channel.AbstractChannel.bind(AbstractChannel.java:260)
>         at 
> io.netty.bootstrap.AbstractBootstrap$2.run(AbstractBootstrap.java:356)
>         at 
> io.netty.util.concurrent.AbstractEventExecutor.runTask(AbstractEventExecutor.java:174)
>         at 
> io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:167)
>         at 
> io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:470)
>         at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:569)
>         at 
> io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997)
>         at 
> io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
>         at 
> org.apache.activemq.artemis.utils.ActiveMQThreadFactory$1.run(ActiveMQThreadFactory.java:118)
> {code}
> This error is correct, but I don't get this error in the broker status:
> {code:java}
> {"configuration":{"properties":{"system":{"alder32":"1"}}},"server":{"state":"STARTED","version":"2.27.1","nodeId":"c83efd88-cf09-11ed-abfe-04cf4b181bdb","identity":null,"uptime":"1.715
>  seconds"}}{code}
> Things I noticed:
> 1. isActive turns false (so I know there is something wrong)
> 2. getStatus gets the status but without showing the error
> 3. The error is not catched, because this is handled by the server
> 4. The version says 2.27.1 though I am running 2.28.0
> My question:
> Can this error be added to the getStatus() method and/or can there be a new 
> method where I can programmatically get this startup error (like 
> getStatupError() or getStartupReport())?
> The reason I need this error is that the broker is run on a server and the 
> user needs to get notified about any errors. Besides using the getStatus() 
> method of the active broker, there maybe already a way to achieve this, but I 
> couldn't figure this out yet. I checked for example 
> [https://activemq.apache.org/components/artemis/documentation/latest/embedding-activemq.html]
>  and the methods on the ActiveMQServerControl class.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to