1 27 28 package simplemessageclient; 29 30 import javax.jms.Connection ; 31 import javax.jms.ConnectionFactory ; 32 import javax.jms.Destination ; 33 import javax.jms.JMSException ; 34 import javax.jms.MessageProducer ; 35 import javax.jms.Queue ; 36 import javax.jms.Session ; 37 import javax.jms.TextMessage ; 38 import javax.naming.Context ; 39 import javax.naming.InitialContext ; 40 import javax.naming.NamingException ; 41 42 46 public class Main { 47 48 49 public Main() { 50 } 51 52 55 public static void main(String [] args) { 56 Context jndiContext = null; 57 ConnectionFactory connectionFactory = null; 58 Connection connection = null; 59 Session session = null; 60 Destination destination = null; 61 MessageProducer messageProducer = null; 62 TextMessage message = null; 63 final int NUM_MSGS = 3; 64 65 try { 66 jndiContext = new InitialContext (); 67 } catch (NamingException e) { 68 System.out.println("Could not create JNDI " + "context: " + 69 e.toString()); 70 System.exit(1); 71 } 72 73 try { 74 connectionFactory = 75 (ConnectionFactory ) jndiContext.lookup( 76 "jms/SimpleMessageDestinationFactory"); 77 destination = 78 (Queue ) jndiContext.lookup("jms/SimpleMessageBean"); 79 } catch (NamingException e) { 80 System.out.println("JNDI lookup failed: " + e.toString()); 81 System.exit(1); 82 } 83 84 try { 85 connection = connectionFactory.createConnection(); 86 session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 87 messageProducer = session.createProducer(destination); 88 message = session.createTextMessage(); 89 90 for (int i = 0; i < NUM_MSGS; i++) { 91 message.setText("This is message " + (i + 1)); 92 System.out.println("Sending message: " + message.getText()); 93 messageProducer.send(message); 94 } 95 96 System.out.println("To see if the bean received the messages,"); 97 System.out.println( 98 " check <install_dir>/domains/domain1/logs/server.log."); 99 } catch (JMSException e) { 100 System.out.println("Exception occurred: " + e.toString()); 101 } finally { 102 if (connection != null) { 103 try { 104 connection.close(); 105 } catch (JMSException e) { 106 } 107 } 108 System.exit(0); 109 } 110 } 111 112 } 113 | Popular Tags |