编程语言
首页 > 编程语言> > java – Spring AMQP:注释驱动的侦听器端点出错

java – Spring AMQP:注释驱动的侦听器端点出错

作者:互联网

我正在尝试设置一个简单的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);
}

应用程序的context.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" />

messageSender,和

@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()));
    }
}

HelloMessage

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

    // Empty constructor, full constructor and accessors
}

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

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

解决方法:

你的问题很简单!

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

public class HelloMessage implements Serializable {
   ....
}

当然,侦听器应用程序应该在其CLASSPATH中具有相同的类,以便能够将有效负载(AMQP消息体)中的byte []反序列化为相应的HelloMessage对象.

标签:spring-amqp,java,spring,rabbitmq
来源: https://codeday.me/bug/20190725/1529276.html