I have two applications for the testing of the MQ events. One of them is used to send messages and another one is used to receive messages. Both of them are SpringBoot applications.
The sender app is running on port: 8000
and receiver app is running on port: 8050
pom.xml
<dependency>
<groupId>com.ibm.mq</groupId>
<artifactId>mq-jms-spring-boot-starter</artifactId>
<version>2.5.0</version>
</dependency>
This is the sender application.
@SpringBootApplication
@EnableJms
@RestController
public class MqTestApplication {
private static final String ISSUER = "254874125487";
private static final String ACCOUNT_NO = "1234567890123";
@Autowired
private JmsTemplate jmsTemplate;
public static void main(String[] args) {
SpringApplication.run(MqTestApplication.class, args);
}
public Event getEvent() {
Event event = new Event();
Map<String, Object> argument = new HashMap<>();
argument.put("key-accNo", ACCOUNT_NO);
event.setMessageId(String.valueOf(UUID.randomUUID()));
event.setIssuer(ISSUER);
event.setType(EventType.ACCOUNT);
event.setName(EventName.OPEN_ACCOUNT);
event.setArgument(argument);
event.setDateTime(LocalDateTime.now());
return event;
}
@GetMapping("send/event")
public void sendEvent() {
try {
jmsTemplate.convertAndSend("DESTINATION", getEvent());
} catch (JmsException ex) {
ex.printStackTrace();
}
}
}
Receiver Application
@SpringBootApplication
@EnableSwagger2
@EnableJms
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@Service
public class MQServiceImpl{
private final JmsTemplate jmsTemplate;
@Autowired
public MQServiceImpl(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
@JmsListener(destination = "DESTINATION")
public void receiveEvent() {
Event event = (Event) jmsTemplate.receiveAndConvert("DESTINATION");
System.out.println(event.toString());
}
}
But when I run the receiver application, I face with below exception rapidly and continuously.
DefaultMessageListenerContainer-1] WARN o.s.j.l.DefaultMessageListenerContainer - Execution of JMS message listener failed, and no ErrorHandler has been set.
org.springframework.jms.listener.adapter.ListenerExecutionFailedException: Listener method 'public void .service.MQServiceImpl.receiveEvent()' threw exception; nested exception is org.springframework.jms.MessageFormatException: JMSCC0053: An exception occurred deserializing a message, exception: 'java.lang.ClassNotFoundException: null class'.; nested exception is com.ibm.msg.client.jms.DetailedMessageFormatException: JMSCC0053: An exception occurred deserializing a message, exception: 'java.lang.ClassNotFoundException: null class'.
It was not possible to deserialize the message because of the exception shown.
Examine the linked exception for details of the error.
Caused by: org.springframework.jms.MessageFormatException: JMSCC0053: An exception occurred deserializing a message, exception: 'java.lang.ClassNotFoundException: null class'.; nested exception is com.ibm.msg.client.jms.DetailedMessageFormatException: JMSCC0053: An exception occurred deserializing a message, exception: 'java.lang.ClassNotFoundException: null class'.
It was not possible to deserialize the message because of the exception shown.
Examine the linked exception for details of the error.
I searched the log but none of those answers helped me. I appreciate it if anybody has an idea to solve this problem.
Update
In both sender and receiver applications, the event class is like this.
@Data
public class Event implements Serializable {
private static final long serialVersionUID = 2529600986924604571L;
private String messageId;
private String issuer;
private EventType type;
private EventName name;
private Map<String, Object> argument;
private LocalDateTime dateTime;
}
After some days, I found out the answer. I should convert the Event object to the JSON and then send it from the sender application and receive the JSON from the sender and convert it to the Event object. Actually, we can't send or receive the objects through MQ directly.
Sender
String stringEvent = objectMapper.writeValueAsString(EVENT_OBJ);
jmsTemplate.convertAndSend(SEND_QUEUE_NAME, stringEvent);
Receiver
@Override
@JmsListener(destination = "${mq.receive_queue}")
public void receive(Message message) {
try {
String jsonEvent = ((TextMessage) message).getText();
Event event = objectMapper.readValue(jsonEvent, Event.class);
} catch (JMSException | IOException e) {
e.printStackTrace();
}
}