Created
June 15, 2012 12:54
-
-
Save mresetar/2936337 to your computer and use it in GitHub Desktop.
Used to test send JMS message in WebSphere
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
*/ | |
package net.croz.jms.test; | |
import java.util.Date; | |
import java.util.Hashtable; | |
import javax.jms.JMSException; | |
import javax.jms.MessageProducer; | |
import javax.jms.Queue; | |
import javax.jms.QueueConnection; | |
import javax.jms.QueueConnectionFactory; | |
import javax.jms.Session; | |
import javax.jms.TextMessage; | |
import javax.naming.Context; | |
import javax.naming.InitialContext; | |
import javax.naming.NamingException; | |
/** | |
* @author mresetar | |
* | |
*/ | |
public class SendJmsMessage { | |
private static final String IIOP_ADDRESS = "iiop://localhost:2812"; | |
private static final String JMS_JNDI_QUEUE_NAME = "jms/testQueue"; | |
private static final String JMS_JND_QUEUE_CONN_FACT_NAME = "jms/testCF"; | |
private void runConnectAndSend() { | |
try { | |
final InitialContext context = setupJndiConnetion(); | |
// Lookup the topic connection factory | |
// from the initial context. | |
QueueConnectionFactory qcf = qcfLookup(context); | |
QueueConnection connection = qcf.createQueueConnection(); | |
Session sess = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); | |
Queue queue = queueLookup(context); | |
sendMessage(sess, queue); | |
} catch (NamingException ne) { | |
ne.printStackTrace(); | |
} catch (JMSException jmse) { | |
Exception linkedException = jmse.getLinkedException(); | |
if (linkedException != null) { | |
linkedException.printStackTrace(); | |
} | |
jmse.printStackTrace(); | |
} | |
} | |
private void sendMessage(Session sess, Queue queue) throws JMSException { | |
System.out.println("Sending message"); | |
MessageProducer producer = sess.createProducer(queue); | |
TextMessage message = sess.createTextMessage(); | |
String text = "Hello World at: " + new Date(); | |
message.setText(text); | |
producer.send(message); | |
System.out.println("Message '" + text + "' sent."); | |
} | |
private Queue queueLookup(final InitialContext context) | |
throws NamingException { | |
System.out.println("Performing lookup of " + JMS_JNDI_QUEUE_NAME); | |
Queue queue = (Queue) context.lookup(JMS_JNDI_QUEUE_NAME); | |
return queue; | |
} | |
private QueueConnectionFactory qcfLookup(final InitialContext context) | |
throws NamingException { | |
System.out.println("Performing lookup of " + JMS_JND_QUEUE_CONN_FACT_NAME); | |
QueueConnectionFactory qcf = (QueueConnectionFactory) context.lookup(JMS_JND_QUEUE_CONN_FACT_NAME); | |
return qcf; | |
} | |
private InitialContext setupJndiConnetion() throws NamingException { | |
System.out.println("Trying to connect to JNDI at: " + IIOP_ADDRESS); | |
// final InitialContext context = new InitialContext(); | |
String icf = "com.ibm.websphere.naming.WsnInitialContextFactory"; | |
// Initialise JNDI properties | |
Hashtable<String, Object> env = new Hashtable<String, Object>(); | |
env.put(Context.INITIAL_CONTEXT_FACTORY, icf); | |
env.put(Context.PROVIDER_URL, IIOP_ADDRESS); | |
final InitialContext context = new InitialContext(env); | |
System.out.println("JNDI conntext was setup. Connected to: " + IIOP_ADDRESS); | |
System.out.println("JNDI reading name in namespace:" + context.getNameInNamespace()); | |
return context; | |
} | |
public static void main(String[] args) throws NamingException { | |
SendJmsMessage sendJmsMessage = new SendJmsMessage(); | |
sendJmsMessage.runConnectAndSend(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment