Sunday 5 August 2018

JMS: MessageNotWriteableException

When a message is received, its body is only readable. If any attempt is made to change the body of a message, a MessageNotWriteableException is thrown.

JMSProducer.java
package com.sample.producer;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public class JMSProducer {
 private static final String JMS_PROVIDER_URL = "tcp://127.0.0.1:5050";
 private static final String QUEUE = "DEMO_QUEUE";

 public static void main(String[] args) throws JMSException {
  ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(JMS_PROVIDER_URL);
  Connection connection = connectionFactory.createConnection();
  connection.start();

  Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

  Destination destination = session.createQueue(QUEUE);

  MessageProducer producer = session.createProducer(destination);

  TextMessage message = session.createTextMessage("Hello, this is my first message.");

  producer.send(message);
  connection.close();
 }
}

JMSConsumer.java
package com.sample.consumer;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public class JMSConsumer {
 private static final String JMS_PROVIDER_URL = "tcp://127.0.0.1:5050";
 private static final String QUEUE = "DEMO_QUEUE";

 public static void main(String args[]) throws JMSException {
  Connection connection = null;

  try {
   ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(JMS_PROVIDER_URL);
   connection = connectionFactory.createConnection();
   connection.start();

   Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

   Destination destination = session.createQueue(QUEUE);

   MessageConsumer consumer = session.createConsumer(destination);

   Message message = consumer.receive();

   if (message instanceof TextMessage) {
    TextMessage textMessage = (TextMessage) message;
    System.out.println("\nReceived message '" + textMessage.getText() + "'");

    ((TextMessage) message).setText("Hello");
   }

  } finally {
   if (connection != null)
    connection.close();
  }

 }

}


Run JMSProvider followed by JMSConsumer, you will end up in 'javax.jms.MessageNotWriteableException'.

Received message 'Hello, this is my first message.'
Exception in thread "main" javax.jms.MessageNotWriteableException: Message body is read-only
         at org.apache.activemq.command.ActiveMQMessage.checkReadOnlyBody(ActiveMQMessage.java:746)
         at org.apache.activemq.command.ActiveMQTextMessage.setText(ActiveMQTextMessage.java:74)
         at com.sample.consumer.JMSConsumer.main(JMSConsumer.java:38)






Previous                                                 Next                                                 Home

No comments:

Post a Comment