无法创建生产者以发送消息至ActiveMQ。

huangapple go评论103阅读模式
英文:

Unable to create producer to send messages on ActiveMQ

问题

我正在学习JMS和不同类型的代理。我目前在一个虚拟项目中使用ActiveMQ(Artemis)。

我目前使用默认设置运行Artemis。我可以进入管理控制台并查看队列和主题。现在我正在创建两个基于Java Spring的应用程序,一个用于生产,一个用于消费。我看过一些教程,但是我遇到了一个NPE(空指针异常),我不确定为什么,因为我认为我正在正确地自动装配bean。

这些是我的类:

主类:

@SpringBootApplication
public class SpringJmsApplication {

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

    SendMessage send = new SendMessage("这是测试消息");
  }
}

发送器:

public class Sender {

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

  @Autowired
  private JmsTemplate jmsTemplate;

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

发送器配置:

@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服务:

public class SendMessage {  
    @Autowired
    Sender sender;

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

基本上,错误源自SendMessage类,它无法自动装配sender bean,但我不确定为什么会发生这个错误,因为Sender bean是在SenderConfig类中创建的,所以Spring应该已将其添加到Spring容器/Bean工厂/应用程序上下文中。

这是堆栈跟踪:

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

希望这能帮助你解决问题。如果有其他问题,请随时提问。

英文:

I am learning JMS and different types of brokers out there. I am currently using ActiveMQ (Artemis) for a dummy project.

I currently have Artemis running on the default settings. I can go to the management console and see the queues and topics.
I am now creating 2 Java Spring-based apps; 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.

These are my classes:

Main class:

@SpringBootApplication
public class SpringJmsApplication {

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

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

Sender:

public class Sender {

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

  @Autowired
  private JmsTemplate jmsTemplate;

  public void send(String message) {
    LOGGER.info(&quot;sending message=&#39;{}&#39;&quot;, message);
    jmsTemplate.convertAndSend(&quot;helloworld.q&quot;, message);
  }
}

Sender Config:

@Configuration
public class SenderConfig {

  @Value(&quot;${artemis.broker-url}&quot;)
  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?

This is the stacktrace:

Exception in thread &quot;main&quot; java.lang.NullPointerException
	at com.codenotfound.jms.SendMessage.&lt;init&gt;(SendMessage.java:11)
	at com.codenotfound.SpringJmsApplication.main(SpringJmsApplication.java:16)

答案1

得分: 1

这是你的主类中的罪魁祸首。

SendMessage send = new SendMessage("这是测试消息");

你正在自己创建对象,而不是从上下文中获取,Spring DI 不会应用于自己创建的对象。

解决方案是,通过注解@ComponentSendMessage标记为Spring管理的Bean,并从上下文中获取它。

英文:

Here is the culprit in your main class.

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

You are creating object yourself instead of getting from the context, Spring DI won't be applied to the objects created by ourself.

Solution is, mark the SendMessage as spring managed bean by annotating with @Component and get it from context.

答案2

得分: 1

Your problem doesn't stem from SendMessage class, this class seems OK.

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; rather, you're creating it manually with a new keyword, as:

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.

英文:

Your problem doesn't stem from SendMessage class, this class seems OK.

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; rather, you're creating it manually with a new keyword, as:

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

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.

huangapple
  • 本文由 发表于 2020年8月1日 20:18:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63205119.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定