1 43 44 import javax.jms.*; 45 import javax.naming.InitialContext ; 46 import javax.naming.Context ; 47 import javax.naming.NamingException ; 48 import javax.transaction.UserTransaction ; 49 50 54 public class SimpleSender extends Thread { 55 56 59 void sendMessages(String msg[], QueueConnectionFactory factory, Queue queue) throws JMSException { 60 QueueConnection connection = factory.createQueueConnection(); 62 QueueSession session = connection.createQueueSession (false, Session.AUTO_ACKNOWLEDGE); 63 QueueSender sender = session.createSender(queue); 64 65 connection.start(); 67 for (int i=0; i<msg.length; i++) { 68 TextMessage message = session.createTextMessage(msg[i]); 69 sender.send(message); 70 System.out.println("[SimpleSender] send : "+message.getText()); 71 } 72 73 connection.stop(); 75 sender.close(); 76 session.close(); 77 connection.close(); 78 } 79 80 public void run() { 81 try { 82 Context ctx = new InitialContext (); 84 QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("theQueueConnectionFactory"); 85 Queue queue = (Queue) ctx.lookup("theQueue"); 86 UserTransaction utx = (UserTransaction )ctx.lookup("UserTransaction"); 87 88 sendMessages(new String []{"non transactional message"},factory,queue); 89 90 utx.begin(); 91 sendMessages(new String []{"transactional message with commit"},factory,queue); 92 utx.commit(); 93 94 utx.begin(); 95 sendMessages(new String []{"transactional message with rollback"},factory,queue); 96 utx.rollback(); 97 98 sendMessages(new String []{"LAST message"},factory,queue); 99 100 } catch (Exception e) { 101 System.err.println("Exception thrown from SimpleSender "+e.getMessage()); 102 e.printStackTrace(); 103 } 104 } 105 106 107 } 108 | Popular Tags |