【问题标题】:Artemis activeMQ embeded not working with spring integration tests嵌入的 Artemis activeMQ 不适用于 Spring 集成测试
【发布时间】:2020-08-30 10:23:27
【问题描述】:

我尝试在 Spring Boot 集成测试中使用带有嵌入式模式的 artemis activeMQ,但是当我发送消息时,它无法接收它。 我使用本机模式,它工作得很好,但当我运行集成测试(嵌入式)时,情况并非如此。

这是我的applications-test.properties(src/test/resources):

spring.artemis.mode=embedded
spring.artemis.host=localhost
spring.artemis.port=61616
spring.artemis.user=exampleuser
spring.artemis.password=examplepassword
spring.artemis.embedded.topics=exampleTopic,exampleTopic2
jms.queue.destination=exampleQueue
spring.jms.pub-sub-domain=true

我的broker.xml.bak(src/test/resources):

<?xml version='1.0'?>
<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="urn:activemq" xsi:schemaLocation="urn:activemq /schema/artemis-server.xsd">
<jms xmlns="urn:activemq:jms">
    <!--the queue used by the example -->
    <queue name="exampleQueue" />
</jms>

<core xmlns="urn:activemq:core">

    <persistence-enabled>false</persistence-enabled>
    <journal-type>NIO</journal-type>

    <!-- Acceptors -->
    <acceptors>
        <acceptor name="netty">tcp://localhost:61616</acceptor>
    </acceptors>

    <!-- Other config -->

    <security-settings>
        <!--security for example queue -->
        <security-setting match="#">
            <permission type="createDurableQueue" roles="example" />
            <permission type="deleteDurableQueue" roles="example" />
            <permission type="createNonDurableQueue" roles="example" />
            <permission type="deleteNonDurableQueue" roles="example" />
            <permission type="consume" roles="example" />
            <permission type="send" roles="example" />
        </security-setting>
    </security-settings>

</core>
</configuration>

生产者 jms 消息类:

@Component
public class JmsProducer {

  @Autowired
  private
  JmsTemplate jmsTemplate;

  @Value("${jms.queue.destination}")
  private String destinationQueue;

  public void send(String msg){
      getJmsTemplate().convertAndSend(destinationQueue, msg);
  }

  public JmsTemplate getJmsTemplate() {
      return jmsTemplate;
  }
 }

这是集成测试类:

@RunWith(SpringRunner.class)
@SpringBootTest
@TestPropertySource(locations = {"/application-test.properties","/broker.xml.bak"})
public class outilConverterApplicationTests {

@Autowired
private JmsProducer producer;

@Autowired
private ArtemisProperties artemisProperties;

@Before
public void init(){
    customizer();
}

public ArtemisConfigurationCustomizer customizer() {
    return new ArtemisConfigurationCustomizer() {

        @Override
        public void customize(org.apache.activemq.artemis.core.config.Configuration configuration) {
            try {
                configuration.addAcceptorConfiguration("netty", "tcp://localhost:" + artemisProperties.getPort());

            } catch (Exception e) {
                throw new RuntimeException("Failed to add netty transport acceptor to artemis instance", e);
            }

        }

    };
}

@Test
public void send(){
    producer.send("MyMessage");
    producer.getJmsTemplate().setReceiveTimeout(20_000);
    assertThat(producer.getJmsTemplate().receiveAndConvert("exampleQueue")).isEqualTo("MyMessage");
}
}

Junit Contole 跟踪错误:

org.junit.ComparisonFailure: expected:<"MyMessage"> but was:<null>

【问题讨论】:

  • 也许你可以在 GH 的某个地方分享一个项目让我们在本地玩?
  • 如果 artemis 类似于 activemq,broker 可能在发送和接收之间停止/启动;坚持是错误的。无论如何,出于性能原因,最好将CachingConnectionFactoryJmsTemplate 一起使用-它将保持操作之间的连接打开。

标签: spring spring-boot activemq-artemis broker


【解决方案1】:

我为 JmsTemplate 设置了 DefaultDestinationName:

jmsTemplate.setDefaultDestinationName("exampleQueue");

我还更改了 application-test.properties:

spring.jms.pub-sub-domain=false

最后一切都很好。

【讨论】:

    猜你喜欢
    • 2021-03-17
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 2019-08-11
    • 2016-01-06
    • 2022-12-08
    • 2018-05-08
    • 2016-12-30
    相关资源
    最近更新 更多