|
| 1 | +/* |
| 2 | + * Copyright © 2017, 2020 IBM Corp. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file |
| 5 | + * except in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the |
| 10 | + * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
| 11 | + * either express or implied. See the License for the specific language governing permissions |
| 12 | + * and limitations under the License. |
| 13 | + */ |
| 14 | + |
| 15 | +// An example of using the Spring Boot JmsTemplate in conjunction with local transactions |
| 16 | +// |
| 17 | +// This program connects to a queue manager, puts a message and then tries to move the message |
| 18 | +// from one queue to another, but choosing to fail that movement through a rollback of the |
| 19 | +// transaction. Despite using a Spring TransactionManager object, there is no distributed (XA or |
| 20 | +// two-phase) transaction created. |
| 21 | +// |
| 22 | +// An equivalent MQI program would have this logic: |
| 23 | +// MQPUT(q1) with SYNCPOINT |
| 24 | +// MQCMIT |
| 25 | +// MQGET(q1) with SYNCPOINT |
| 26 | +// MQPUT(q2) with SYNCPOINT |
| 27 | +// MQBACK |
| 28 | + |
| 29 | +package sample2; |
| 30 | + |
| 31 | +import java.util.Date; |
| 32 | + |
| 33 | +import javax.jms.Message; |
| 34 | + |
| 35 | +import org.springframework.boot.SpringApplication; |
| 36 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 37 | +import org.springframework.context.ConfigurableApplicationContext; |
| 38 | +import org.springframework.jms.annotation.EnableJms; |
| 39 | +import org.springframework.jms.connection.JmsTransactionManager; |
| 40 | +import org.springframework.jms.core.JmsTemplate; |
| 41 | +import org.springframework.transaction.TransactionStatus; |
| 42 | +import org.springframework.transaction.annotation.EnableTransactionManagement; |
| 43 | + |
| 44 | +@SpringBootApplication |
| 45 | +@EnableJms |
| 46 | +@EnableTransactionManagement |
| 47 | +public class Application { |
| 48 | + |
| 49 | + static final String qName1 = "DEV.QUEUE.1"; // A queue from the default MQ Developer container config |
| 50 | + static final String qName2 = "DEV.QUEUE.2"; // Another queue from the default MQ Developer container config |
| 51 | + |
| 52 | + public static void main(String[] args) { |
| 53 | + |
| 54 | + // Launch the application |
| 55 | + ConfigurableApplicationContext context = SpringApplication.run(Application.class, args); |
| 56 | + |
| 57 | + // Create a transaction manager object that will be used to control commit/rollback of operations. |
| 58 | + JmsTransactionManager tm = new JmsTransactionManager(); |
| 59 | + |
| 60 | + printStarted(); |
| 61 | + |
| 62 | + // Create the JMS Template object to control connections and sessions. |
| 63 | + JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); |
| 64 | + |
| 65 | + // Associate the connection factory with the transaction manager |
| 66 | + tm.setConnectionFactory(jmsTemplate.getConnectionFactory()); |
| 67 | + |
| 68 | + // This starts a new transaction scope. "null" can be used to get a default transaction model |
| 69 | + TransactionStatus status = tm.getTransaction(null); |
| 70 | + |
| 71 | + // Create a single message with a timestamp |
| 72 | + String outMsg = "Hello from IBM MQ at " + new Date(); |
| 73 | + |
| 74 | + // The default SimpleMessageConverter class will be called and turn a String |
| 75 | + // into a JMS TextMessage which we send to qName1. This operation will be made |
| 76 | + // part of the transaction that we initiated. |
| 77 | + jmsTemplate.convertAndSend(qName1, outMsg); |
| 78 | + |
| 79 | + // Commit the transaction so the message is now visible |
| 80 | + tm.commit(status); |
| 81 | + |
| 82 | + // But now we're going to start a new transaction to hold multiple operations. |
| 83 | + status = tm.getTransaction(null); |
| 84 | + // Read it from the queue where we just put it, and then send it straight on to |
| 85 | + // a different queue |
| 86 | + Message inMsg = jmsTemplate.receive(qName1); |
| 87 | + jmsTemplate.convertAndSend(qName2, inMsg); |
| 88 | + // This time we decide to rollback the transaction so the receive() and send() are |
| 89 | + // reverted. We end up with the message still on qName1. |
| 90 | + tm.rollback(status); |
| 91 | + |
| 92 | + System.out.println("Done."); |
| 93 | + } |
| 94 | + |
| 95 | + static void printStarted() { |
| 96 | + System.out.println(); |
| 97 | + System.out.println("========================================"); |
| 98 | + System.out.println("MQ JMS Transaction Sample started."); |
| 99 | + System.out.println("========================================"); |
| 100 | + } |
| 101 | +} |
0 commit comments