【问题标题】:Spring Boot client to connect to existing JBoss EAP7 ActiveMQSpring Boot 客户端连接到现有的 JBoss EAP7 ActiveMQ
【发布时间】:2017-11-02 16:29:20
【问题描述】:

我一直在网上搜索,但找不到有关如何使用 Spring-Boot 客户端连接到 JBoss 上的 ActiveMQ(启动并运行)的示例。

有很多来自 Spring 的教程,但使用的是嵌入式代理。

任何指针都会很棒!

提前致谢, 机器学习

根据提供的信息,我得到了这个:

@Configuration
@SpringBootApplication
@EnableJms
public class Application {

@Bean
ActiveMQConnectionFactory activeMQConnectionFactory() {

    final String host = "http://127.0.0.1:8080";

    ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(host);
    factory.setUserName("user");
    factory.setPassword("pwd");
    factory.setTrustAllPackages(true);


    return factory;
}

@Bean
public JmsListenerContainerFactory<?> myFactory(ConnectionFactory connectionFactory,
        DefaultJmsListenerContainerFactoryConfigurer configurer) {
    DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
    // This provides all boot's default to this factory, including the
    // message converter
    configurer.configure(factory, connectionFactory);
    // You could still override some of Boot's default if necessary.
    return factory;
}



@Bean // Serialize message content to json using TextMessage
public MessageConverter jacksonJmsMessageConverter() {
    MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
    converter.setTargetType(MessageType.TEXT);
    converter.setTypeIdPropertyName("_type");
    return converter;
}

public static void main(String[] args) {
    // Launch the application
    ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);

    JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);

    // Send a message with a POJO - the template reuse the message converter
    System.out.println("Sending an email message.");
    jmsTemplate.convertAndSend("TestQ", new Email("info@example.com", "Hello"));
}

}

我在 pom.xml 中的依赖项:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-activemq</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-optional</artifactId>
        <version>5.7.0</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
    </dependency>
</dependencies>

但我有这个例外:

2017-06-01 17:55:54.176  INFO 2053 --- [           main] hello.Application                        : Started Application in 2.02 seconds (JVM running for 5.215)
Sending an email message.
[WARNING] 
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:527)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.jms.UncategorizedJmsException: Uncategorized exception occurred during JMS processing; nested exception is javax.jms.JMSException: Could not create Transport. Reason: java.lang.IllegalArgumentException: Invalid connect parameters: {wireFormat.host=127.0.0.1}
    at org.springframework.jms.support.JmsUtils.convertJmsAccessException(JmsUtils.java:316)
    at org.springframework.jms.support.JmsAccessor.convertJmsAccessException(JmsAccessor.java:169)
    at org.springframework.jms.core.JmsTemplate.execute(JmsTemplate.java:487)
    at org.springframework.jms.core.JmsTemplate.send(JmsTemplate.java:570)
    at org.springframework.jms.core.JmsTemplate.convertAndSend(JmsTemplate.java:658)
    at hello.Application.main(Application.java:69)
    ... 6 more
Caused by: javax.jms.JMSException: Could not create Transport. Reason: java.lang.IllegalArgumentException: Invalid connect parameters: {wireFormat.host=127.0.0.1}
    at org.apache.activemq.util.JMSExceptionSupport.create(JMSExceptionSupport.java:36)
    at org.apache.activemq.ActiveMQConnectionFactory.createTransport(ActiveMQConnectionFactory.java:333)
    at org.apache.activemq.ActiveMQConnectionFactory.createActiveMQConnection(ActiveMQConnectionFactory.java:346)
    at org.apache.activemq.ActiveMQConnectionFactory.createActiveMQConnection(ActiveMQConnectionFactory.java:304)
    at org.apache.activemq.ActiveMQConnectionFactory.createConnection(ActiveMQConnectionFactory.java:244)
    at org.springframework.jms.support.JmsAccessor.createConnection(JmsAccessor.java:180)
    at org.springframework.jms.core.JmsTemplate.execute(JmsTemplate.java:474)
    ... 9 more
Caused by: java.lang.IllegalArgumentException: Invalid connect parameters: {wireFormat.host=127.0.0.1}
    at org.apache.activemq.transport.TransportFactory.doConnect(TransportFactory.java:126)
    at org.apache.activemq.transport.TransportFactory.connect(TransportFactory.java:65)
    at org.apache.activemq.ActiveMQConnectionFactory.createTransport(ActiveMQConnectionFactory.java:331)
    ... 14 more

我不知道这是什么:

Caused by: java.lang.IllegalArgumentException: Invalid connect 
parameters: {wireFormat.host=127.0.0.1}

谢谢

【问题讨论】:

    标签: spring-boot jboss activemq


    【解决方案1】:

    对于配置,但将代理 url 更改为您的外部 ActiveMQ 服务器:请参阅 http://activemq.apache.org/uri-protocols.html 了解不同的协议(例如 tcp)。您可能还需要提供用户名和密码。

    我从本教程 https://spring.io/guides/gs/messaging-jms/ 开始,并进行了自己的编辑以使其以我想要的方式与 ActiveMQ 一起工作,您也可以在此处查看其他选项:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-messaging.html。在 Spring 中有几种不同的方法可以做到这一点。

    还包括工厂 bean。从这里开始,我使用 @JmsListener 注解来监听消息并使用 jmstemplate 来发布消息。

    import javax.jms.ConnectionFactory;
    
    import org.apache.activemq.ActiveMQConnectionFactory;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.boot.autoconfigure.jms.DefaultJmsListenerContainerFactoryConfigurer;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.jms.annotation.EnableJms;
    import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
    import org.springframework.jms.config.JmsListenerContainerFactory;  
    
    
    
    @Configuration
    @EnableJms
    public class JMSConfiguration {
        private static final Logger logger = LoggerFactory.getLogger(JMSConfiguration.class);
    
        @Bean ActiveMQConnectionFactory activeMQConnectionFactory() {
            ActiveMQConnectionFactory factory =
                    new ActiveMQConnectionFactory("tcp://somehost:61616");
    
            factory.setTrustAllPackages(true);
    
            return factory;
        }
    
        @Bean
        public JmsListenerContainerFactory<?> myFactory(ConnectionFactory connectionFactory,
                DefaultJmsListenerContainerFactoryConfigurer configurer) {
            DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
            // This provides all boot's default to this factory, including the
            // message converter
            configurer.configure(factory, connectionFactory);
            // You could still override some of Boot's default if necessary.
            return factory;
        }
    }
    

    依赖:spring-boot-starter-activemq

    春季版:1.4.3.RELEASE

    【讨论】:

      猜你喜欢
      • 2018-07-26
      • 1970-01-01
      • 2015-11-17
      • 2010-09-17
      • 1970-01-01
      • 2015-06-05
      • 2013-09-06
      • 1970-01-01
      • 2014-12-12
      相关资源
      最近更新 更多