【问题标题】:Spring AMQP: error with annotation-driven listener endpointsSpring AMQP:注释驱动的侦听器端点错误
【发布时间】:2014-11-24 11:02:55
【问题描述】:

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

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

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

【问题讨论】:

    标签: java spring rabbitmq spring-amqp


    【解决方案1】:

    你的问题很简单!

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

    public class HelloMessage implements Serializable {
       ....
    }
    

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

    【讨论】:

    • 哇,这确实很愚蠢。我跳过了消息转换器一章。所以当然要消除这个限制并使用简单的 POJO,最好使用更好的转换器,JSON 是最有可能的候选者。尝试应用此操作时的一个小问题:必须在 amqpTemplate(发送端)和侦听器容器(接收端)上设置 msg 转换器。
    猜你喜欢
    • 1970-01-01
    • 2017-09-01
    • 2018-01-15
    • 2016-06-03
    • 1970-01-01
    • 2014-02-09
    • 1970-01-01
    • 2011-06-03
    • 1970-01-01
    相关资源
    最近更新 更多