kagamihogeの日記

kagamihogeの日記です。

Spring BootでApache ActiveMQの送受信

Spring Bootでhttp://activemq.apache.org/を使用しての送受信のhello worldレベルのことをやる。

手順など

Apache ActiveMQのインストール

http://activemq.apache.org/をインストールする。まずbin/artemis create 名前を実行する。以下はbin/artemis create mybrokerと実行したときの様子。

apache-artemis-2.10.1\bin>artemis.cmd create mybroker
Creating ActiveMQ Artemis instance at: C:\(中略)\apache-artemis-2.10.1\bin\mybroker

--user: is a mandatory property!
Please provide the default username:
kagami

--password: is mandatory with this configuration:
Please provide the default password:


--allow-anonymous | --require-login: is a mandatory property!
Allow anonymous access?, valid values are Y,N,True,False
Y

Auto tuning journal ...
done! Your system can make 0.95 writes per millisecond, your journal-buffer-timeout will be 1056000

You can now start the broker by executing:

   "(略)\apache-artemis-2.10.1\bin\mybroker\bin\artemis" run

Or you can setup the broker as Windows service and run it in the background:

   "(略)\apache-artemis-2.10.1\bin\mybroker\bin\artemis-service.exe" install
   "(略)\apache-artemis-2.10.1\bin\mybroker\bin\artemis-service.exe" start

   To stop the windows service:
      "(略)\apache-artemis-2.10.1\bin\mybroker\bin\artemis-service.exe" stop

   To uninstall the windows service
      "(略)\apache-artemis-2.10.1\bin\mybroker\bin\artemis-service.exe" uninstall

bin\mybroker\にファイル等が作られる。起動するにはmybroker\bin\artemis runする。実行時のログは以下。

apache-artemis-2.10.1\bin\mybroker\bin>artemis run
     _        _               _
    / \  ____| |_  ___ __  __(_) _____
   / _ \|  _ \ __|/ _ \  \/  | |/  __/
  / ___ \ | \/ |_/  __/ |\/| | |\___ \
 /_/   \_\|   \__\____|_|  |_|_|/___ /
 Apache ActiveMQ Artemis 2.10.1


2020-01-07 16:28:31,509 INFO  [org.apache.activemq.artemis.integration.bootstrap] AMQ101000: Starting ActiveMQ Artemis Server
2020-01-07 16:28:31,558 INFO  [org.apache.activemq.artemis.core.server] AMQ221000: live Message Broker is starting with configuration Broker Configuration (clustered=false,journalDirectory=data/journal,bindingsDirectory=data/bindings,largeMessagesDirectory=data/large-messages,pagingDirectory=data/paging)
2020-01-07 16:28:31,588 INFO  [org.apache.activemq.artemis.core.server] AMQ221013: Using NIO Journal
2020-01-07 16:28:31,626 INFO  [org.apache.activemq.artemis.core.server] AMQ221057: Global Max Size is being adjusted to 1/2 of the JVM max size (-Xmx). being defined as 536,870,912
2020-01-07 16:28:31,655 INFO  [org.apache.activemq.artemis.core.server] AMQ221043: Protocol module found: [artemis-server]. Adding protocol support for: CORE
2020-01-07 16:28:31,656 INFO  [org.apache.activemq.artemis.core.server] AMQ221043: Protocol module found: [artemis-amqp-protocol]. Adding protocol support for: AMQP
2020-01-07 16:28:31,657 INFO  [org.apache.activemq.artemis.core.server] AMQ221043: Protocol module found: [artemis-hornetq-protocol]. Adding protocol support for: HORNETQ
2020-01-07 16:28:31,657 INFO  [org.apache.activemq.artemis.core.server] AMQ221043: Protocol module found: [artemis-mqtt-protocol]. Adding protocol support for: MQTT
2020-01-07 16:28:31,658 INFO  [org.apache.activemq.artemis.core.server] AMQ221043: Protocol module found: [artemis-openwire-protocol]. Adding protocol support for: OPENWIRE
2020-01-07 16:28:31,659 INFO  [org.apache.activemq.artemis.core.server] AMQ221043: Protocol module found: [artemis-stomp-protocol]. Adding protocol support for: STOMP
2020-01-07 16:28:31,743 INFO  [org.apache.activemq.artemis.core.server] AMQ221034: Waiting indefinitely to obtain live lock
2020-01-07 16:28:31,744 INFO  [org.apache.activemq.artemis.core.server] AMQ221035: Live Server Obtained live lock
2020-01-07 16:28:32,804 INFO  [org.apache.activemq.artemis.core.server] AMQ221080: Deploying address DLQ supporting [ANYCAST]
2020-01-07 16:28:32,804 INFO  [org.apache.activemq.artemis.core.server] AMQ221003: Deploying ANYCAST queue DLQ on address DLQ
2020-01-07 16:28:32,805 INFO  [org.apache.activemq.artemis.core.server] AMQ221080: Deploying address ExpiryQueue supporting [ANYCAST]
2020-01-07 16:28:32,805 INFO  [org.apache.activemq.artemis.core.server] AMQ221003: Deploying ANYCAST queue ExpiryQueue on address ExpiryQueue
2020-01-07 16:28:33,925 INFO  [org.apache.activemq.artemis.core.server] AMQ221020: Started NIO Acceptor at 0.0.0.0:61616 for protocols [CORE,MQTT,AMQP,STOMP,HORNETQ,OPENWIRE]
2020-01-07 16:28:33,961 INFO  [org.apache.activemq.artemis.core.server] AMQ221020: Started NIO Acceptor at 0.0.0.0:5445 for protocols [HORNETQ,STOMP]
2020-01-07 16:28:33,992 INFO  [org.apache.activemq.artemis.core.server] AMQ221020: Started NIO Acceptor at 0.0.0.0:5672 for protocols [AMQP]
2020-01-07 16:28:34,024 INFO  [org.apache.activemq.artemis.core.server] AMQ221020: Started NIO Acceptor at 0.0.0.0:1883 for protocols [MQTT]
2020-01-07 16:28:34,051 INFO  [org.apache.activemq.artemis.core.server] AMQ221020: Started NIO Acceptor at 0.0.0.0:61613 for protocols [STOMP]
2020-01-07 16:28:34,058 INFO  [org.apache.activemq.artemis.core.server] AMQ221007: Server is now live
2020-01-07 16:28:34,059 INFO  [org.apache.activemq.artemis.core.server] AMQ221001: Apache ActiveMQ Artemis Message Broker version 2.10.1 [0.0.0.0, nodeID=100ac6af-3114-11ea-8aac-d89ef337aa82]
2020-01-07 16:28:34,517 INFO  [org.apache.activemq.hawtio.branding.PluginContextListener] Initialized activemq-branding plugin
2020-01-07 16:28:34,791 INFO  [org.apache.activemq.hawtio.plugin.PluginContextListener] Initialized artemis-plugin plugin
2020-01-07 16:28:37,100 INFO  [io.hawt.HawtioContextListener] Initialising hawtio services
2020-01-07 16:28:37,147 INFO  [io.hawt.system.ConfigManager] Configuration will be discovered via system properties
2020-01-07 16:28:37,155 INFO  [io.hawt.jmx.JmxTreeWatcher] Welcome to hawtio 1.5.5 : http://hawt.io/ : Don't cha wish your console was hawt like me? ;-)
2020-01-07 16:28:37,165 INFO  [io.hawt.jmx.UploadManager] Using file upload directory: (略)\apache-artemis-2.10.1\bin\mybroker\tmp\uploads
2020-01-07 16:28:37,248 INFO  [io.hawt.web.AuthenticationFilter] Starting hawtio authentication filter, JAAS realm: "activemq" authorized role(s): "amq" role principal classes: "org.apache.activemq.artemis.spi.core.security.jaas.RolePrincipal"
2020-01-07 16:28:37,366 INFO  [io.hawt.web.JolokiaConfiguredAgentServlet] Jolokia overridden property: [key=policyLocation, value=file:(略)/apache-artemis-2.10.1/bin/mybroker/etc/\jolokia-access.xml]
2020-01-07 16:28:37,856 INFO  [io.hawt.web.RBACMBeanInvoker] Using MBean [hawtio:type=security,area=jmx,rank=0,name=HawtioDummyJMXSecurity] for role based access control
2020-01-07 16:28:57,017 INFO  [io.hawt.system.ProxyWhitelist] Initial proxy whitelist: [localhost, 127.0.0.1, 10.48.52.34, p47187.intra.rakuten.co.jp, 172.17.231.161, P47187.mshome.net, 10.0.75.1, P47187.intra.rakuten.co.jp, 10.80.90.213, 192.168.56.1, kubernetes.docker.internal]
2020-01-07 16:28:57,968 INFO  [org.apache.activemq.artemis] AMQ241001: HTTP Server started at http://localhost:8161
2020-01-07 16:28:57,968 INFO  [org.apache.activemq.artemis] AMQ241002: Artemis Jolokia REST API available at http://localhost:8161/console/jolokia
2020-01-07 16:28:57,969 INFO  [org.apache.activemq.artemis] AMQ241004: Artemis Console available at http://localhost:8161/console
2020-01-07 16:29:02,076 INFO  [io.hawt.web.keycloak.KeycloakServlet] Keycloak integration is disabled
2020-01-07 16:29:12,886 INFO  [io.hawt.web.LoginServlet] hawtio login is using 1800 sec. HttpSession timeout
2020-01-07 16:29:46,430 WARN  [org.apache.activemq.artemis.core.server] AMQ222149: Message Reference[633]:RELIABLE:CoreMessage[messageID=633,durable=true,userID=cdb5084f-311c-11ea-8aac-d89ef337aa82,priority=4, timestamp=Tue Jan 07 16:10:36 JST 2020,expiration=0, durable=true, address=remotingQueue,size=1530,properties=TypedProperties[__HDR_dlqDeliveryFailureCause=java.lang.Throwable: Dispatch[7] to ID:P47187-64333-1578382186053-1:1:1:1 exceeds redelivery policy limit:RedeliveryPolicy {destination = null, collisionAvoidanceFactor = 0.15, maximumRedeliveries = 6, maximumRedeliveryDelay = -1, initialRedeliveryDelay = 1000, useCollisionAvoidance = false, useExponentialBackOff = false, backOffMultiplier = 5.0, redeliveryDelay = 1000, preDispatchCheck = true},__AMQ_CID=ID:P47187-63745-1578381036655-0:1,_AMQ_GROUP_SEQUENCE=0,__HDR_BROKER_IN_TIME=1578381036826,_AMQ_ROUTING_TYPE=1,__HDR_ARRIVAL=0,__HDR_COMMAND_ID=5,__HDR_PRODUCER_ID=[0000 0035 7B01 0021 4944 3A50 3437 3138 372D 3633 3734 352D 3135 3738 3338 3130 3336 3635 352D 313A 3100 0000 0000 0000 0100 0000 0000 0000 01),__HDR_MESSAGE_ID=[0000 0048 6E00 017B 0100 2149 443A 5034 3731 3837 2D36 3337 3435 2D31 3537  ...  0000 0000 0001 0000 0000 0000 0001 0000 0000 0000 0001 0000 0000 0000 0000),__HDR_DROPPABLE=false]]@828366005 has reached maximum delivery attempts, sending it to Dead Letter Address DLQ from remotingQueue
2020-01-07 16:30:08,654 WARN  [org.apache.activemq.artemis.core.client] AMQ212037: Connection failure to /127.0.0.1:64334 has been detected: An existing connection was forcibly closed by the remote host [code=GENERIC_EXCEPTION]
2020-01-07 16:30:38,355 WARN  [org.apache.activemq.artemis.core.client] AMQ212037: Connection failure to /127.0.0.1:64343 has been detected: An existing connection was forcibly closed by the remote host [code=GENERIC_EXCEPTION]
2020-01-07 16:44:50,527 WARN  [org.apache.activemq.artemis.core.client] AMQ212037: Connection failure to /127.0.0.1:64353 has been detected: An existing connection was forcibly closed by the remote host [code=GENERIC_EXCEPTION]

ログの最後の方にある通り http://localhost:8161/console で管理コンソールが開ける。id/passwordはartemis create xxxxx時に入力したものを使う。

これでhttp://activemq.apache.org/のインストールと起動は完了。

spring-bootからApache ActiveMQを使う

build.gradle

plugins {
    id 'org.springframework.boot' version '2.2.2.RELEASE'
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
    id 'java'
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-artemis'
}

プロパティ

application.ymlなどに接続先urlを指定する。

spring:
  activemq:
    broker-url: tcp://localhost:61616

送信

とりあえず適当な文字列を送信する。

import javax.jms.Queue;

import org.apache.activemq.artemis.jms.client.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.core.JmsTemplate;

@SpringBootApplication
public class Application implements CommandLineRunner {

    @Bean
    Queue queue() {
        return new ActiveMQQueue("remotingQueue");
    }
    
    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).run(args);
    }
    
    @Autowired
    private Queue queue;

    @Autowired
    private JmsTemplate jmsTemplate;

    @Override
    public void run(String... args) throws Exception {
        jmsTemplate.convertAndSend(queue, "message");
        System.exit(0);
    }
}

受信

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.jms.annotation.JmsListener;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).run(args);
    }

    @JmsListener(destination = "remotingQueue")
    public void listener(String message){
        System.out.println("Message received " + message);;
    }
}