javaactivemq-artemishawtio

Embedded ActiveMQ Artemis doesn't show web console


I am a junior Java developer, and I made a simple sample of embedded ActiveMQ Artemis programmatic. It works correctly, but I want to monitor it on my browser on http://localost:8161 (like ActiveMQ Artemis that I install and run standalone). However, in embedded mode it didn't give me the web console, and I can't find any correct sample on any where to see it. Somewhere people say about Hawtio, JMX, and other ways but they didn't work correctly. Can somebody give a simple demo to me please?

JMX Hawtio and other things didn't work.

This is my simple class :

import org.apache.activemq.artemis.core.config.impl.ConfigurationImpl;
import org.apache.activemq.artemis.core.server.embedded.EmbeddedActiveMQ;

public class EmbeddedActiveMq {

    public static void main(String[] args) throws Exception {

        ConfigurationImpl config = new ConfigurationImpl()
                .addAcceptorConfiguration("in-vm", "vm://0")
                .addAcceptorConfiguration("tcp", "tcp://localhost:61616")
                .setJMXManagementEnabled(true);

        EmbeddedActiveMQ server = new EmbeddedActiveMQ();
        server.setConfiguration(config);

        server.start();
        System.out.println("artemis is ok");
        while (true) {

        }
    }
}

These are my dependencies:

<dependencies>
    <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>artemis-server</artifactId>
        <version>2.16.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>artemis-jms-server</artifactId>
        <version>2.16.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>artemis-jms-client</artifactId>
        <version>2.16.0</version>
    </dependency>
    <dependency>
        <groupId>org.jolokia</groupId>
        <artifactId>jolokia-core</artifactId>
        <version>1.7.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>artemis-core-client</artifactId>
        <version>2.17.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>artemis-web</artifactId>
        <version>2.29.0</version>
    </dependency>
    <dependency>
        <groupId>io.hawt</groupId>
        <artifactId>hawtio-default</artifactId>
        <version>2.12.0</version>
        <type>war</type>
    </dependency>
</dependencies>

Update:

I used your solution, Mr. Bertram, and now I have a little problem. It listens on port 8161, but it seems I did something wrong:

enter image description here

enter image description here

For pathToArtemis i use this path: .m2/repository/org/apache/activemq/artemis-server/2.16.0/artemis-server-2.16.0.jar and this path : .m2/repository/org/apache/activemq/artemis-web/2.16.0/artemis-web-2.16.0.jar and the installation directory of artemis, but none of them did work, and I get nothing in panel.


Solution

  • You can enable the management console via an embedded instance of Jetty managed by the broker using the following code:

    System.setProperty("hawtio.realm", "hawtio");
    System.setProperty("hawtio.role", "myRole");
    System.setProperty("hawtio.rolePrincipalClasses", "org.apache.activemq.artemis.spi.core.security.jaas.RolePrincipal");
    System.setProperty("java.security.auth.login.config", "/path/to/login.config");
    String pathToArtemis = "/path/to/artemis";
    ConfigurationImpl config = new ConfigurationImpl()
            .addAcceptorConfiguration("in-vm", "vm://0")
            .addAcceptorConfiguration("tcp", "tcp://localhost:61616")
            .setJMXManagementEnabled(true);
    EmbeddedActiveMQ server = new EmbeddedActiveMQ();
    server.setMbeanServer(ManagementFactory.getPlatformMBeanServer());
    server.start();
    WebServerComponent webServerComponent = new WebServerComponent();
    WebServerDTO webServerDTO = new WebServerDTO();
    webServerDTO.path = "web";
    BindingDTO bindingDTO = new BindingDTO();
    bindingDTO.uri = "http://localhost:8161";
    AppDTO branding = new AppDTO();
    branding.name = "branding";
    branding.url = "activemq-branding";
    branding.war = "activemq-branding.war";
    AppDTO plugin = new AppDTO();
    plugin.name = "plugin";
    plugin.url = "artemis-plugin";
    plugin.war = "artemis-plugin.war";
    AppDTO console = new AppDTO();
    console.name = "console";
    console.url = "console";
    console.war = "console.war";
    bindingDTO.addApp(branding);
    bindingDTO.addApp(plugin);
    bindingDTO.addApp(console);
    webServerDTO.addBinding(bindingDTO);
    ActiveMQSecurityManager securityManager = server.getActiveMQServer().getSecurityManager();
    ManagementContextDTO managementDTO = new ManagementContextDTO();
    ManagementContext managementContext = ManagementFactory.create(managementDTO, securityManager);
    server.getActiveMQServer().getManagementService().registerHawtioSecurity(managementContext.getArtemisMBeanServerGuard());
    webServerComponent.configure(webServerDTO, pathToArtemis, pathToArtemis);
    server.getActiveMQServer().addExternalComponent(webServerComponent, true);
    

    Your login.config might look something like this:

    hawtio {
       org.apache.activemq.artemis.spi.core.security.jaas.PropertiesLoginModule required
           debug=false
           reload=true
           org.apache.activemq.jaas.properties.user="hawtio-users.properties"
           org.apache.activemq.jaas.properties.role="hawtio-roles.properties";
    };
    

    hawtio-users.properties:

    myUser=myPass
    

    hawtio-roles.properties:

    myRole=myUser
    

    Then you can login to the console at http://localhost:8161/console with the user myUser and the password myPass.

    Lastly, I believe you can simplify your dependencies down to this:

    <dependencies>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>artemis-server</artifactId>
            <version>2.16.0</version>
        </dependency>
    </dependencies>