Spring AMQP: bug with annotation triggered listener endpoints

I'm trying to set up a simple Spring AMQP script, but I get the following error:

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]]

      

I don't understand the error message, I was impressed, I can use any POJO to send and receive a message as per the documentation here .

This is a very simple setup:

Main

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 applications

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

      

MessageListener

@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
}

      

I can verify that the message is indeed sent and is in the queue:

enter image description here

Default RabbitMQ installation via homebrew.

+3


source to share


1 answer


Your problem is very simple!

To send any Java object through RabbitMQ with a default SimpleMessageConverter

, you must mark your class as Serializable

:



public class HelloMessage implements Serializable {
   ....
}

      

Of course, an application listener

must have the same class in its CLASSPATH in order to be able deserialize

byte[]

from payload

(AMQP message body) to the appropriate object HelloMessage

.

+7


source







All Articles