0

I have a code that reads from the IBM MQ queue manager but I want to be read from IBM MQ without removing the message from the queue, only after I send acknowledge to IBM MQ I want to remove the message

this is my IBM reader code :

public class IBMReaderStub extends AbstractReader {
    private JMSContext context = null;
    JMSConsumer consumer;
    Destination destination;
    
    public IBMReaderStub(String queueName) {
        this(queueName, new IBMListener());
    }
   
    public IBMReaderStub(String queueName, IBMListener onMessage) {
        super(ConfigurationManager.getString(HOST), ConfigurationManager.getInt(PORT, DEFAULT_IBM_PORT), queueName, new QueueWithThreadPool(), onMessage);
    }
   
    @Override
    protected void initializeConsumer() {
        try {
            JmsConnectionFactory jmsConnectionFactory = createJmsConnectionFactory();
            context = jmsConnectionFactory.createContext();
            destination = context.createQueue("queue:///" + getQueueName()); // Set the producer and consumer destination to be the same... not true in general
            consumer = context.createConsumer(destination);
        } catch (Exception e) {
            System.out.println(e);
        }
        listen();
    }

    @Override
    public void listen() {
        consumer.setMessageListener(getOnMessage());
    }

    private JmsConnectionFactory createJmsConnectionFactory() throws Exception {
        JmsFactoryFactory jmsFactory = JmsFactoryFactory.getInstance(WMQConstants.WMQ_PROVIDER);
        JmsConnectionFactory jmsConnectionFactory = jmsFactory.createConnectionFactory();
        jmsConnectionFactory.setStringProperty(WMQConstants.WMQ_HOST_NAME, this.getHost());
        jmsConnectionFactory.setIntProperty(WMQConstants.WMQ_PORT, getPort());
        jmsConnectionFactory.setStringProperty(WMQConstants.WMQ_CHANNEL, ConfigurationManager.getString(CHANNEL_NAME));
        jmsConnectionFactory.setStringProperty(WMQConstants.WMQ_QUEUE_MANAGER, ConfigurationManager.getString(QUEUE_MANAGER_NAME));
        jmsConnectionFactory.setStringProperty(WMQConstants.WMQ_APPLICATIONNAME, ConfigurationManager.getString(APPLICATION_NAME));
        jmsConnectionFactory.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT);
        return jmsConnectionFactory;
    }
    
    public static void main(String[] args) {
        try {
            IBMReaderStub reader = new IBMReaderStub("hey");
            IBMReaderStub reader2 = new IBMReaderStub("hey");
            reader.listen();
            reader2.listen();

        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

1 Answers1

1

IBM MQ provides transactional access to messages, so you need to create a transacted session, and then you can commit or roll back message gets or puts as needed.

https://www.ibm.com/support/knowledgecenter/SSFKSJ_9.1.0/com.ibm.mq.dev.doc/q032210_.html

https://www.ibm.com/support/knowledgecenter/SSFKSJ_8.0.0/com.ibm.mq.dev.doc/q032220_.htm

Attila Repasi
  • 1,803
  • 10
  • 11