Tomcat 无法与 Artemis MQ 通信

Tomcat failing to communicate with Artemis MQ

我想使用 JMS 安排消息延迟。为此,我将 JMS 2.0 与 Artemis MQ 一起使用,并且在编译时成功安排了消息,但是当我在 Tomcat 中部署它并尝试延迟发送消息时,它说:

java.lang.IllegalStateException: setDeliveryDelay requires JMS 2.0

我猜测 Tomcat 无法与我设置的 Artemis 服务器正常通信。所以我在我的系统中设置了两个服务器:Tomcat 和 Artemis。是因为 Tomcat 无法与 Artemis 通讯吗?如果是这样,那么为什么我的 spring 启动内置 Tomcat 服务器检测到 Artemis 而我的独立 Tomcat 却没有?

我的 JMS 消息发送器源代码是:

@Service
public class TransferServiceImpl implements TransferService {

    @Autowired
    private JmsTemplate jmsTemplate;

    @Override
    public void transfertoJMS(Transaction transaction) {
        System.out.println("Sending a transaction.");

        jmsTemplate.setDeliveryDelay(20000);
        jmsTemplate.convertAndSend(destinationQueue, transaction);
    }
}

我的pom.xml是:

 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.stargate.transferfund</groupId>
<artifactId>TransferFund</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<name>TransferFund</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <startClass>org.springframework.boot.SpringApplication</startClass>
    <maven.test.skip>true</maven.test.skip>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <!-- for test case, in memory db -->
    <dependency>
        <groupId>org.hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <scope>test</scope>
    </dependency>
    <!-- marked the embedded servlet container as provided -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <!-- JMS related dependencies -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-artemis</artifactId>
        <version>1.5.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>artemis-junit</artifactId>
        <version>1.5.5</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-broker</artifactId>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
    </dependency>

    <!-- http://repo1.maven.org/maven2/commons-lang/commons-lang/2.6/commons-lang-2.6.jar -->
    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.6</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

我的application.properties是:

server.port=${port:8085}

# database config
spring.datasource.url=jdbc:mysql://localhost:3306/ach_stargate
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=none

# jms config
jms.queue.destination=TransactionQueue
spring.artemis.mode=native
spring.artemis.host=localhost
spring.artemis.port=61616
spring.artemis.user=admin
spring.artemis.password=admin

您与两个相互冲突的依赖项发生了冲突。以下依赖项 is for ActiveMQ 5.x(!):

<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-broker</artifactId>
</dependency>

Spring 可能会从该依赖项中找到 JMS 1.0 spec,该依赖项会覆盖类路径上的 JMS 2.0 规范,并在 spring 时为您提供 java.lang.IllegalStateException查找启动 ConnectionFactory.

时要使用的必要 JMS 接口

如果您想要一个嵌入式代理,spring-boot-starter-artemis 已经包含它,因此您应该能够毫无问题地删除 activemq-broker 依赖项。