Spring启动Artemis嵌入式代理行为

问题描述 投票:6回答:3

大家早,

我最近一直在与spring-boot-artemis-starter挣扎。我对它的spring-boot支持的理解如下:

  • 设置spring.artemis.mode=embedded,和tomcat一样,spring-boot将通过tcp(服务器模式)实现可以访问的代理。以下命令应该成功:nc -zv localhost 61616
  • 设置spring.artmis.mode=native和spring-boot只会根据spring.artemis.*属性(客户端模式)配置jms模板。

客户端模式可以在我的机器上使用独立的artemis服务器。不幸的是,我无法在服务器模式下设法到达tcp端口。

如果有人确认我对嵌入式模式的理解,我将不胜感激。

谢谢您的帮助

经过一番挖掘后,我注意到spring-boot-starter-artemis开箱即用的实现使用了org.apache.activemq.artemis.core.remoting.impl.invm.InVMAcceptorFactory接受器。我想知道这不是根本原因(我再也不是专家)。但似乎有一种方法可以自定义artemis配置。因此我尝试了以下配置而没有任何运气:

@SpringBootApplication
public class MyBroker {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(MyBroker.class, args);
    }

    @Autowired
    private ArtemisProperties artemisProperties;

    @Bean
    public ArtemisConfigurationCustomizer artemisConfigurationCustomizer() {
        return configuration -> {
            try {
               configuration.addAcceptorConfiguration("netty", "tcp://localhost:" + artemisProperties.getPort());
            } catch (Exception e) {
                throw new RuntimeException("Failed to add netty transport acceptor to artemis instance");
            }
        };
    }

}
spring-boot broker activemq-artemis
3个回答
6
投票

您只需要为Artemis配置添加连接器和接受器。使用Spring Boot Artemis starter Spring创建一个Configuration bean,用于EmbeddedJMS配置。您可以在ArtemisEmbeddedConfigurationFactory类中看到这一点,其中将为配置设置InVMAcceptorFactory。您可以编辑此bean并通过自定义ArtemisConfigurationCustomizer bean更改Artemis行为,该bean将被Spring autoconfig吸取并应用于Configuration。

Spring Boot应用程序的示例配置类:

import org.apache.activemq.artemis.api.core.TransportConfiguration;
import org.apache.activemq.artemis.core.remoting.impl.netty.NettyAcceptorFactory;
import org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnectorFactory;
import org.springframework.boot.autoconfigure.jms.artemis.ArtemisConfigurationCustomizer;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ArtemisConfig implements ArtemisConfigurationCustomizer {
    @Override
    public void customize(org.apache.activemq.artemis.core.config.Configuration configuration) {
        configuration.addConnectorConfiguration("nettyConnector", new TransportConfiguration(NettyConnectorFactory.class.getName()));
        configuration.addAcceptorConfiguration(new TransportConfiguration(NettyAcceptorFactory.class.getName()));
    }
}

4
投票

我的同事和我有完全相同的问题,因为关于this link (chapter Artemis Support)的文档没有说添加个人ArtemisConfigurationCustomizer - 这很难过,因为我们意识到如果没有这个定制器,我们的Spring Boot应用程序将启动并表现得好像一切都好,但实际上它不会'做任何事。

我们还意识到,如果没有Customizer,application.properties文件就不会被加载,因此无论你提到的主机或端口是什么,它都不会计算在内。

按照两个示例所述添加定制程序后,它可以正常工作。

这里有一些我们想出的结果:

  • 它仅在配置ArtemisConfigurationCustomizer后加载了application.properties
  • 使用嵌入式spring boot artemis客户端,您不再需要broker.xml
  • 许多显示使用Artemis的示例使用“in-vm”协议,而我们只是想使用netty tcp协议,因此我们需要将其添加到配置中
  • 对我来说,最重要的参数是pub-sub-domain,因为我使用的是主题,而不是队列。如果您正在使用主题,则此参数需要设置为true,否则JMSListener将不会读取消息。

看此页:stackoverflow jmslistener-usage-for-publish-subscribe-topic

使用@JmsListener时,它使用DefaultMessageListenerContainer扩展JmsDestinationAccessor,默认情况下将pubSubDomain设置为false。当此属性为false时,它将在队列上运行。如果要使用主题,则必须将此属性值设置为true。

In Application.properties:
spring.jms.pub-sub-domain=true

如果有人对完整示例感兴趣,我已将其上传到我的github:https://github.com/CorDharel/SpringBootArtemisServerExample


1
投票

嵌入式模式启动代理作为应用程序的一部分。此类设置没有可用的网络协议,只允许InVM调用。自动配置exposes the necessary pieces you can tune虽然我不确定你实际上可以有嵌入模式的TCP / IP通道。

© www.soinside.com 2019 - 2024. All rights reserved.