gpt4 book ai didi

java - Spring boot 和嵌入式 activemq 主机配置

转载 作者:行者123 更新时间:2023-11-30 06:10:39 24 4
gpt4 key购买 nike

我有一个 spring boot 应用程序,它从客户端接收 stomp over websocket 主题订阅,这些订阅将被路由到我的嵌入式 activemq 代理。

我启动嵌入式 activemq 代理的代码

@SpringBootApplication
public class RttApplication {

public static void main(String[] args) throws Exception {
ConfigurableApplicationContext ctx = SpringApplication.run(RttApplication.class, args);
BrokerService brokerService = new BrokerService();
brokerService.setPersistent(false);
brokerService.setUseJmx(false);
brokerService.addConnector("vm://localhost:0");
brokerService.setBrokerName("event");
brokerService.start();
}

}

我的spring broker中继配置类

@Configuration
@EnableWebSocketMessageBroker
public class MessageBrokerConfigurer extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/event").withSockJS();
}

@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableStompBrokerRelay("/topic").setRelayHost("vm://localhost").setRelayPort(0);
registry.setApplicationDestinationPrefixes("/app");
}
}

但是当我启动应用程序时它显示了这个

2016-02-25 15:44:34.678 INFO 7604 --- [ main] o.a.activemq.broker.TransportConnector : Connector vm://localhost:0 Started

2016-02-25 15:44:34.694 INFO 7604 --- [ main] o.apache.activemq.broker.BrokerService : Apache ActiveMQ 5.7.0 (event, ID:PC13082-53189-1456386274543-0:1) started

2016-02-25 15:44:34.694 INFO 7604 --- [ main] o.apache.activemq.broker.BrokerService : For help or more information please see: http://activemq.apache.org

2016-02-25 15:44:39.532 INFO 7604 --- [eactor-tcp-io-2] r.io.net.impl.netty.tcp.NettyTcpClient : Failed to connect to vm://localhost:0. Attempting reconnect in 5000ms.

最佳答案

问题已解决,因为 Spring 配置器方法暗示它是一个 stomp broker 中继,它必须通过 stomp 协议(protocol)。

传输协议(protocol)前缀显然也不是必需的。如果在默认的 activemq 安装中设置了用户名和密码,我还需要输入用户名和密码。但这是在启动一个独立的 ActiveMQ 之后完成的,我实际上正在寻找的是一个嵌入式解决方案。

@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableStompBrokerRelay("/topic")
.setRelayHost("127.0.0.1")
.setRelayPort(61613)
.setClientLogin("system")
.setClientPasscode("password")
registry.setApplicationDestinationPrefixes("/app");

}

更新

回应 Deinum 的上述评论之一。我还尝试在 spring boot 应用程序的 application.properties 中简单地设置以下内容:

spring.activemq.broker-url=stomp://127.0.0.1:61614
spring.activemq.user=system
spring.activemq.password=password

但是控制台没有显示 ActiveMQ 正在启动的迹象,我也无法通过上面发布的 stomp broker 中继配置连接到它。我最终创建了一个 spring 配置类,它现在可以工作了:

//@Configuration
public class TestBrokerConfig {

@Bean( initMethod = "start", destroyMethod = "stop" )
public BrokerService broker() throws Exception {
final BrokerService broker = new BrokerService();
broker.addConnector( "stomp://localhost:61614" );

broker.setPersistent( false );
broker.setShutdownHooks( Collections.< Runnable >singletonList( new SpringContextHook() ) );
final ManagementContext managementContext = new ManagementContext();
managementContext.setCreateConnector( true );
broker.setManagementContext( managementContext );

return broker;
}
}

关于java - Spring boot 和嵌入式 activemq 主机配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35621475/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com