I am writing JmsAdapter where 2 methods named as sendMessage
for sending messages & receiveMessage
for receiving message in spring boot, I have one service class EventService
which uses this JmsAdapter to send message. Now I am confused what to do with message when received in JMSAdapter as i don't want to put any business logic(ex. db call, message processing) in listener receiveMessage
method. I am doing this to have business logic separated from JMS adapter Here is code for JMSAdapter -
public class JmsAdapter {
@Autowired
private JmsTemplate jmsTemplate;
public void sendMessage(final String jsonMessage) throws JMSException {
System.out.println("Sending message = " + jsonMessage);
//TODO replace the queue name based from DB
jmsTemplate.convertAndSend("sender", jsonMessage);
System.out.println("Message Sent");
}
@JmsListener(destination = "${receiver}")
public String receiveMessage(final Message jsonMessage) throws JMSException {
System.out.println("Received message " + jsonMessage);
String response = null;
if(jsonMessage instanceof TextMessage) {
TextMessage textMessage = (TextMessage)jsonMessage;
response = textMessage.getText();
System.out.println("Message Received = "+response );
}
return response ;
}
Do i need to think in different way. What other options are there? As there will be cyclic dependency on Adapter and Service.
I believe the ideal way is to to separate out the responsibility to send and receive messages. A class having more than one responsibility tends to create these kind of confusions. If you don't have reservations on using EJBs then a Message Driven Bean can be used to listen to the incoming messages. If not through EJB then you should have a separate class which is responsible for listening message from this particular queue and process the message. To separate out the responsibility even further, you could call a method(this has all the business logic) implemented in a POJO from your listener.