gpt4 book ai didi

java - Spring AMQP : error with annotation-driven listener endpoints

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:09:33 25 4
gpt4 key购买 nike

我正在尝试设置一个简单的 Spring AMQP 场景,但出现以下错误:

Could not resolve method parameter at index 0 in method:
public void handleMessage(HelloMessage),
with 1 error(s): [Error in object 'msg': codes []; arguments [];
default message [@Payload param is required]]

我不明白错误消息,我的印象是我可以根据文档使用任何 POJO 发送和接收消息 here .

这是一个非常简单的设置:

主要

public class Program {
private static ConfigurableApplicationContext applicationContext;

public static void main(String[] args) {
try {
startApp();
System.out.println("Running...");
System.in.read();
applicationContext.close();
System.out.println("Shutting down...");
}
catch (Throwable e) {
e.printStackTrace();
}
}

private static void startApp() {
applicationContext = new ClassPathXmlApplicationContext("/application-context.xml");
applicationContext.refresh();

MessageSender messageSender = applicationContext.getBean(MessageSender.class);
messageSender.sendMessage("hello", 1);
}

应用程序上下文.xml

    <context:component-scan base-package="org.abiri.amqpTest" />


<rabbit:connection-factory id="connectionFactory"
host="localhost" port="5672"
username="guest" password="guest"/>

<rabbit:template id="amqpTemplate" connection-factory="connectionFactory"/>

<rabbit:admin connection-factory="connectionFactory"/>

<rabbit:queue name="hello"/>

<bean id="rabbitListenerContainerFactory"
class="org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="concurrentConsumers" value="3"/>
<property name="maxConcurrentConsumers" value="10"/>
</bean>

<rabbit:annotation-driven container-factory="rabbitListenerContainerFactory"/>

<rabbit:listener-container connection-factory="connectionFactory" />

消息发送者

@Service
public class MessageSender {

@Autowired
private AmqpTemplate amqpTemplate;

// Accessors...

public void sendMessage(String message, Integer sillyNumber) {
amqpTemplate.convertAndSend("hello",
new HelloMessage(message, sillyNumber));
}
}

消息监听器

@Component
public class MessageListener {

@RabbitListener(queues = "hello")
public void handleMessage(HelloMessage msg) {
out.println(format("Received message: %s with silly number: %d",
msg.getMessage(), msg.getSillyNumber()));
}
}

你好消息

public class HelloMessage {
private String message;
private Integer sillyNumber;

// Empty constructor, full constructor and accessors
}

我能够验证消息确实已发送并且在队列中:

enter image description here

通过自制软件默认安装 RabbitMQ。

最佳答案

你的问题很简单!

要使用默认的 SimpleMessageConverter 通过 RabbitMQ 发送任何 Java 对象,您必须将您的类标记为 Serializable:

public class HelloMessage implements Serializable {
....
}

当然,listener 应用程序应该在其 CLASSPATH 中具有相同的类,以便能够从 反序列化 byte[] >payload(AMQP 消息正文)到适当的 HelloMessage 对象。

关于java - Spring AMQP : error with annotation-driven listener endpoints,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27103311/

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