i have following code to read the messages in the specified queue at consumer end. Basically , i wan to read the message first in the queue and based on message content , do certain things to decide what to send in reply ... here is the code
public class RPCServer {
public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
SpringApplication.run(RPCServer.class, args);
}
private final static Logger logger = LoggerFactory.getLogger(RPCServer.class);
private static final String RPC_QUEUE_NAME = "rpc_queue1";
public RPCServer() throws IOException, TimeoutException {
}
@Bean
public Queue queue() {
return new Queue(RPC_QUEUE_NAME);
}
@Component
public static class RpcListener {
@RabbitListener(queues = RPC_QUEUE_NAME)
public String reply() throws IOException, TimeoutException, ClassNotFoundException, SQLException {
RabbitTemplate rabbitTemplate = null;
@SuppressWarnings("null")
Object message = rabbitTemplate.receive(RPC_QUEUE_NAME);
logger.info("Sent Message was: " + message);
// some processing
return response;
}
}
}
Not getting what is the issue in code. using spring AMQP core framework for all coding. getting NPE at following line:
Object message = rabbitTemplate.receive(RPC_QUEUE_NAME);
here is the stacktrace :
Caused by: java.lang.NullPointerException: null
at com.infy.ci.unitdbamqpservice.RPCServer$RpcListener.reply(RPCServer.java:49) ~[classes!/:0.0.1-SNAPSHOT]
at sun.reflect.GeneratedMethodAccessor28.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_151]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_151]
at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:180) ~[spring-messaging-4.3.14.RELEASE.jar!/:4.3.14.RELEASE]
at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:112) ~[spring-messaging-4.3.14.RELEASE.jar!/:4.3.14.RELEASE]
at org.springframework.amqp.rabbit.listener.adapter.HandlerAdapter.invoke(HandlerAdapter.java:49) ~[spring-rabbit-1.7.6.RELEASE.jar!/:na]
at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:126) ~[spring-rabbit-1.7.6.RELEASE.jar!/:na]
... 12 common frames omitted
further, as per the doc, if the queue does not have any messages , it returns null but i confirmed that, messages are there is queue and ready to consume. but still NPE please suggest
Regards,
RabbitTemplate rabbitTemplate = null;
@SuppressWarnings("null")
Object message = rabbitTemplate.receive(RPC_QUEUE_NAME);
Of course you get an NPE, since the template is null
!
Why do you have a template there at all?
I already explained in this answer that you need a parameter on your reply()
method...
@RabbitListener(queues = RPC_QUEUE_NAME)
public String reply(String request) throws IOException, TimeoutException, ClassNotFoundException, SQLException {
logger.info(request);
return request.toUpperCase();
}
When there is no argument, the framework just calls your method with no data from the message.