Springboot集成activeMQ接收发送对象报错问题及解决方法

4 篇文章 0 订阅
1 篇文章 0 订阅

Springboot集成activeMQ接收发送对象报错问题及解决方法

activeMQ接收发送对象报错问题及解决方法

报错信息:

org.springframework.jms.listener.adapter.ListenerExecutionFailedException: Listener method 'public void com.test.openapi.workshop.service.listeners.QueueConsumerListener.readEmptyContainerActiveQueue(java.lang.Object) throws javax.jms.JMSException' threw exception; nested exception is javax.jms.JMSException: Failed to build body from content. Serializable class not available to broker. Reason: java.lang.ClassNotFoundException: Forbidden class com.test.openapi.repository.model.dto.EmptyContainerGateOutDTO! This class is not trusted to be serialized as ObjectMessage payload. Please take a look at http://activemq.apache.org/objectmessage.html for more information on how to configure trusted classes.
	at org.springframework.jms.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:122)
	at org.springframework.jms.listener.adapter.MessagingMessageListenerAdapter.onMessage(MessagingMessageListenerAdapter.java:77)
	at org.springframework.jms.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:736)
	at org.springframework.jms.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:696)
	at org.springframework.jms.listener.AbstractMessageListenerContainer.doExecuteListener(AbstractMessageListenerContainer.java:674)
	at org.springframework.jms.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:645)
	at org.springframework.jms.listener.SimpleMessageListenerContainer.processMessage(SimpleMessageListenerContainer.java:345)
	at org.springframework.jms.listener.SimpleMessageListenerContainer.lambda$createListenerConsumer$2(SimpleMessageListenerContainer.java:322)
	at org.apache.activemq.ActiveMQMessageConsumer.dispatch(ActiveMQMessageConsumer.java:1404)
	at org.apache.activemq.ActiveMQSessionExecutor.dispatch(ActiveMQSessionExecutor.java:131)
	at org.apache.activemq.ActiveMQSessionExecutor.iterate(ActiveMQSessionExecutor.java:202)
	at org.apache.activemq.thread.PooledTaskRunner.runTask(PooledTaskRunner.java:133)
	at org.apache.activemq.thread.PooledTaskRunner$1.run(PooledTaskRunner.java:48)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)

代码:

@Configuration
public class ActiveMqBeanConfig {

    @Value("${spring.activemq.broker-url}")
    private String brokerUrl;
    @Value("${spring.activemq.user}")
    private String username;
    @Value("${spring.activemq.password}")
    private String password;
    @Value("${spring.activemq.queue-name-empty}")
    private  String emptyQueue;
    @Value("${spring.activemq.queue-name-full}")
    private  String fullQueue;
    @Value("${spring.activemq.topic-name}")
    private  String topic;



    public  Queue emptyQueue() {
        return new ActiveMQQueue(emptyQueue);
    }


    public  Queue fullQueue() {
        return new ActiveMQQueue(fullQueue);
    }


    public  Topic topic() {
        return new ActiveMQTopic(topic);
    }

    @Bean
    public ConnectionFactory connectionFactory() {
        return new ActiveMQConnectionFactory(username, password, brokerUrl);
    }

    @Bean
    public JmsMessagingTemplate jmsMessageTemplate() {
        return new JmsMessagingTemplate(connectionFactory());
    }


    /**
     * 在Queue模式中,对消息的监听需要对containerFactory进行配置
     *
     * @param connectionFactory
     * @return
     */
    @Bean("queueListener")
    public JmsListenerContainerFactory<?> queueJmsListenerContainerFactory(ConnectionFactory connectionFactory) {
        SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setPubSubDomain(false);
        return factory;
    }


    /**
     * 在Topic模式中,对消息的监听需要对containerFactory进行配置
     *
     * @param connectionFactory
     * @return
     */
    @Bean("topicListener")
    public JmsListenerContainerFactory<?> topicJmsListenerContainerFactory(ConnectionFactory connectionFactory) {
        SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setPubSubDomain(true);
        return factory;
    }

}

发送:

@RestController
@RequestMapping(value = "/testApi")
@Api(tags = "(测试对外接口)- TestApi endpoints")
@Slf4j
public class TestApiEndpoint {
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    private ActiveMqBeanConfig activeMqBeanConfig;

    @GetMapping(value = "/testApi", produces = MediaType.TEXT_PLAIN_VALUE)
    @ApiOperation(value = "测试api", notes = "测试api")
    @MixmicroResponseBody(direct = true)
    public String testApi() throws JMSException {
        EmptyContainerGateOutDTO emptyContainerGateOutDTO = new EmptyContainerGateOutDTO();
        emptyContainerGateOutDTO.setEirId("test");
        log.info("[==] {}", emptyContainerGateOutDTO);
        this.sendMessage(activeMqBeanConfig.emptyQueue(), emptyContainerGateOutDTO);
        return "success";
    }

    /**
     * 发送消息,destination是发送到的队列,message是待发送的消息
     *
     * @param destination
     * @param obj
     */
    public void sendMessage(Destination destination, Object obj) throws JMSException {
        ActiveMQObjectMessage activeMQObjectMessage = new ActiveMQObjectMessage();
        activeMQObjectMessage.setObject((Serializable) obj);
        jmsMessagingTemplate.convertAndSend(destination, activeMQObjectMessage);
    }

}

监听:

@JmsListener(destination = "${spring.activemq.queue-name-empty}", containerFactory = "queueListener")
    public void readEmptyContainerActiveQueue(Object message) throws JMSException {
        ActiveMQObjectMessage activeMQObjectMessage = (ActiveMQObjectMessage)message;

        EmptyContainerGateOutDTO emptyContainerGateOutDTO = (EmptyContainerGateOutDTO)activeMQObjectMessage.getObject();
        System.out.println("queue接受到:" +  emptyContainerGateOutDTO);

    }

然后看了下官方文档 大致意思就是为了安全官方给出了,设置了白名单的功能

http://activemq.apache.org/objectmessage.html
官方
1

方法一:在客户端设置下 按照官方文档来就好了,由于这个MQ不是我自己在用,这个方法就不试了
方法二:
①某些包设置白名单

 //白名单的包名
        activeMQObjectMessage.setTrustedPackages(new ArrayList(Arrays.asList("class com.test.openapi.repository.model.dto".split(","))));

信任部分
②信任全部

activeMQObjectMessage.setTrustAllPackages(true); //需要设置信任哪些包  //设置这样表示信任所有

信任全部

两种都可以解决ActiveMQ发送接收对象的报错问题

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Spring Boot 集成 ActiveMQ 很简单,只需要在项目的 pom.xml 文件中添加 ActiveMQ 的依赖,然后在配置文件 application.properties 中配置 ActiveMQ 的地址即可。具体步骤如下: 1. 在 pom.xml 文件中添加 ActiveMQ 的依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> ``` 2. 在 application.properties 文件中配置 ActiveMQ 的地址,例如: ``` spring.activemq.broker-url=tcp://localhost:61616 ``` 3. 在启动类中添加 @EnableJms 注解,表示启用 JMS。 4. 创建一个 JmsTemplate 对象发送接收消息。 完成以上步骤即可在 Spring Boot 中使用 ActiveMQ。 ### 回答2: Spring BootSpring Framework的一个用于简化Spring应用程序开发和部署的框架。它提供了一种简单的方式来开发、构建和运行Spring应用程序,同时也能方便地集成其他框架和技术。 ActiveMQ是一个流行的开源消息代理,它支持多种协议和编程语言,包括Java、C#、C++、Python等。它提供了可靠的消息传递机制,可以用于构建分布式系统和异步消息处理应用。 Spring Boot提供了对ActiveMQ集成支持,可以通过在pom.xml文件中添加相关依赖和配置,实现快速集成和使用ActiveMQ。 下面是详细的步骤: 1. 在pom.xml文件中添加ActiveMQ的依赖: ``` <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> </dependencies> ``` 2. 在application.properties文件中添加ActiveMQ的配置: ``` spring.activemq.broker-url=tcp://localhost:61616 spring.activemq.user=admin spring.activemq.password=admin ``` 其中broker-url是ActiveMQ的连接地址,user和password是登录信息。 3. 在代码中使用JmsTemplate发送接收消息: ``` @Service public class MessageService { @Autowired private JmsTemplate jmsTemplate; public void sendMessage(String message) { jmsTemplate.send("test.queue", new MessageCreator() { public Message createMessage(Session session) throws JMSException { return session.createTextMessage(message); } }); } @JmsListener(destination = "test.queue") public void receiveMessage(String message) { System.out.println("Received message: " + message); } } ``` 上面的代码定义了一个MessageService类,里面包含了一个sendMesssge方法和一个receiveMessage方法。sendMessage方法将消息发送到名为“test.queue”的队列中,而receiveMessage方法则监听该队列并接收消息。 在使用JmsTemplate发送消息时,需要提供消息的目的地和消息内容。这里使用createTextMessage方法创建一个文本消息,并将其发送到指定的队列中。 在使用@JmsListener注解接收消息时,需要指定监听的队列名称和接收消息的方法接收到消息后,将会调用该方法并输出消息内容。 4. 运行应用程序并测试消息发送接收功能。 启动应用程序后,调用MessageService的sendMessage方法即可发送消息到ActiveMQ,同时在控制台输出了接收到的消息。这样,就成功地实现了Spring BootActiveMQ集成。 总之,通过以上步骤,我们可以很容易地将Spring BootActiveMQ进行集成,在开发分布式系统和消息处理应用时,能够方便地使用ActiveMQ提供的可靠消息传递机制。 ### 回答3: Spring Boot是一种基于Spring框架的快速开发框架,它提供了很多便捷快捷的特性。ActiveMQ则是一种高性能、开源的消息传递中间件。Spring BootActiveMQ的结合,能够搭建一个可插拔、可扩展、可重复利用的消息解决方案。下面将介绍如何在Spring Boot集成ActiveMQ。 1、引入所需依赖 首先需要在项目中引入ActiveMQ相关的依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> 2、配置ActiveMQ 在application.properties文件中配置ActiveMQ,如下所示: #ActiveMQ configuration spring.activemq.broker-url=tcp://localhost:61616 spring.activemq.user=admin spring.activemq.password=admin spring.jms.template.default-destination=test-queue spring.jms.listener.auto-startup=true 其中,spring.activemq.broker-url指定了ActiveMQ服务器的地址;spring.activemq.user和spring.activemq.password是登录ActiveMQ服务器的用户名和密码;spring.jms.template.default-destination设置了默认的目的地队列;spring.jms.listener.auto-startup设置消息监听器的自动启动。 3、编写消息生产者代码 @Inject private JmsMessagingTemplate jmsMessagingTemplate; public void produceMessage(String message) { jmsMessagingTemplate.convertAndSend("test-queue", message); } 首先在类中注入JmsMessagingTemplate,它是Spring Boot提供的用于发送消息的工具类。然后编写produceMessage()方法,将消息发送到目的地队列test-queue中。 4、编写消息消费者代码 @JmsListener(destination="test-queue") public void receiveMessage(String message) { System.out.println("Received message: " + message); } 使用@JmsListener注解,在方法上指定监听的目的地队列为test-queue。当有消息到达test-queue时,就会自动调用receiveMessage()方法,进行消息处理。 通过上述步骤,就可以在Spring Boot集成ActiveMQ,实现消息生产者和消息消费者的功能。当然,还可以通过其他方式来实现集成,例如使用ConnectionFactory或者JmsTemplate等Spring集成方式。这样就能够应用在更多的项目中,提高应用的可靠性和可扩展性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值