简体   繁体   English

无法创建生产者在 ActiveMQ 上发送消息

[英]Unable to create producer to send messages on ActiveMQ

I am learning JMS and different types of brokers out there.我正在学习 JMS 和不同类型的经纪人。 I am currently using ActiveMQ (Artemis) for a dummy project.我目前正在为一个虚拟项目使用 ActiveMQ (Artemis)。

I currently have Artemis running on the default settings.我目前在默认设置下运行 Artemis。 I can go to the management console and see the queues and topics.我可以 go 到管理控制台并查看队列和主题。 I am now creating 2 Java Spring-based apps;我现在正在创建 2 个 Java 基于 Spring 的应用程序; one for producing and one for consuming.一种用于生产,一种用于消费。 I have seen few tutorials out there, but I'm getting a NPE, which I'm not sure - why, as I believe I am autowiring the bean correctly.我在那里看过很少的教程,但是我得到了一个 NPE,我不确定 - 为什么,因为我相信我正确地自动装配了 bean。

These are my classes:这些是我的课:

Main class:主class:

@SpringBootApplication
public class SpringJmsApplication {

  public static void main(String[] args) {
    SpringApplication.run(SpringJmsApplication.class, args);

    SendMessage send = new SendMessage("This is the test message");
  }
}

Sender:发件人:

public class Sender {

  private static final Logger LOGGER =
      LoggerFactory.getLogger(Sender.class);

  @Autowired
  private JmsTemplate jmsTemplate;

  public void send(String message) {
    LOGGER.info("sending message='{}'", message);
    jmsTemplate.convertAndSend("helloworld.q", message);
  }
}

Sender Config:发件人配置:

@Configuration
public class SenderConfig {

  @Value("${artemis.broker-url}")
  private String brokerUrl;

  @Bean
  public ActiveMQConnectionFactory senderActiveMQConnectionFactory() {
    return new ActiveMQConnectionFactory(brokerUrl);
  }

  @Bean
  public CachingConnectionFactory cachingConnectionFactory() {
    return new CachingConnectionFactory(
        senderActiveMQConnectionFactory());
  }

  @Bean
  public JmsTemplate jmsTemplate() {
    return new JmsTemplate(cachingConnectionFactory());
  }

  @Bean
  public Sender sender() {
    return new Sender();
  }
}

SendMessage Service:发送消息服务:

public class SendMessage {  
    @Autowired
    Sender sender;

    public SendMessage(String message){
        this.sender.send(message);
    }
}

So essentially the error is stemming from the SendMessage class, it's unable to autowire the sender bean but I'm not sure why this error is happening because the Sender bean is being created in the SenderConfig class hence surely Spring should've added it to it Spring container/Bean factory/application context?所以基本上错误源于 SendMessage class,它无法自动连接发送方 bean,但我不确定为什么会发生此错误,因为发送方 bean 是在 SenderConfig class 中创建的,因此 Z38008DD81C2F4D798 肯定应该添加它Spring 容器/Bean 工厂/应用程序上下文?

This is the stacktrace:这是堆栈跟踪:

Exception in thread "main" java.lang.NullPointerException
    at com.codenotfound.jms.SendMessage.<init>(SendMessage.java:11)
    at com.codenotfound.SpringJmsApplication.main(SpringJmsApplication.java:16)

Here is the culprit in your main class.这是您的主要 class 的罪魁祸首。

SendMessage send = new SendMessage("This is the test message");

You are creating object yourself instead of getting from the context, Spring DI won't be applied to the objects created by ourself.您正在自己创建 object 而不是从上下文中获取, Spring DI不会应用于我们自己创建的对象。

Solution is, mark the SendMessage as spring managed bean by annotating with @Component and get it from context.解决方案是,通过使用@Component注释将SendMessage标记为 spring 托管 bean 并从上下文中获取它。

Your problem doesn't stem from SendMessage class, this class seems OK.您的问题不是来自SendMessage class,这个 class 似乎没问题。

Your NPE is caused by the way you obtain an instance of SendMessage class, namely, you're not really obtaining the @Bean , managed by Spring Container;您的 NPE 是由您获取SendMessage class 实例的方式引起的,即您并没有真正获得由@Bean容器管理的 @Bean ; rather, you're creating it manually with a new keyword, as:相反,您使用new关键字手动创建它,如下所示:

SendMessage send = new SendMessage("This is the test message");

This allocates a completely new object in the Heap, which is not going to end up in the Spring Container, hence → is not going to be managed by Spring, hence → its field sender will not be @Autowired .这会在堆中分配一个new的 object,它不会最终出现在 Spring 容器中,因此 → 不会由 Spring 管理,因此 → 它的字段sender不会是@Autowired

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM