1 25 26 package org.objectweb.easybeans.examples.messagedrivenbean; 27 28 import java.util.Hashtable ; 29 30 import javax.jms.Queue ; 31 import javax.jms.QueueConnection ; 32 import javax.jms.QueueConnectionFactory ; 33 import javax.jms.QueueSender ; 34 import javax.jms.QueueSession ; 35 import javax.jms.Session ; 36 import javax.jms.TextMessage ; 37 import javax.naming.Context ; 38 import javax.naming.InitialContext ; 39 import javax.naming.NamingException ; 40 41 45 public final class ClientMessageDriven { 46 47 50 private static final String QUEUE_CONNECTION_FACTORY = "JQCF"; 51 52 55 private static final String SAMPLE_QUEUE = "SampleQueue"; 56 57 60 private static final int NUMBER_MESSAGES = 5; 61 62 65 private static final String DEFAULT_INITIAL_CONTEXT_FACTORY = "org.objectweb.carol.jndi.spi.MultiOrbInitialContextFactory"; 66 67 70 private ClientMessageDriven() { 71 72 } 73 74 79 public static void main(final String [] args) throws Exception { 80 81 Context initialContext = getInitialContext(); 83 84 QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory ) initialContext 86 .lookup(QUEUE_CONNECTION_FACTORY); 87 88 Queue queue = (Queue ) initialContext.lookup(SAMPLE_QUEUE); 90 91 QueueConnection queueConnection = queueConnectionFactory.createQueueConnection(); 93 94 QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); 96 97 QueueSender queueSender = queueSession.createSender(queue); 99 100 TextMessage message = null; 102 for (int i = 0; i < NUMBER_MESSAGES; i++) { 103 message = queueSession.createTextMessage(); 104 String text = "Message_" + i; 105 message.setText(text); 106 queueSender.send(message); 107 System.out.println("Message [" + message.getJMSMessageID() + ", text:" + text + "] Sended"); 108 109 } 110 111 queueSender.close(); 113 queueSession.close(); 114 queueConnection.close(); 115 116 } 117 118 122 private static Context getInitialContext() throws NamingException { 123 124 Hashtable <String , Object > env = new Hashtable <String , Object >(); 128 env.put(Context.INITIAL_CONTEXT_FACTORY, getInitialContextFactory()); 129 130 133 return new InitialContext (env); 134 } 135 136 141 private static String getInitialContextFactory() { 142 String prop = System.getProperty("easybeans.client.initial-context-factory"); 143 if (prop == null) { 145 prop = DEFAULT_INITIAL_CONTEXT_FACTORY; 146 } 147 return prop; 148 } 149 150 } 151 | Popular Tags |