0% found this document useful (0 votes)
36 views20 pages

Creating JMS Servers

This document discusses how to create and test JMS servers and resources in WebLogic. It provides code samples to send and receive messages from a JMS queue. The document explains how to create a JMS server targeted to a managed server in WebLogic. It describes adding connection factories, queues, and topics as JNDI resources to the JMS system module. Code samples are given to get the initial context, session, send text messages to a queue, and receive and print messages with a listener. The receiver notifies and quits if the received message equals "quit".

Uploaded by

Atish Dabholkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views20 pages

Creating JMS Servers

This document discusses how to create and test JMS servers and resources in WebLogic. It provides code samples to send and receive messages from a JMS queue. The document explains how to create a JMS server targeted to a managed server in WebLogic. It describes adding connection factories, queues, and topics as JNDI resources to the JMS system module. Code samples are given to get the initial context, session, send text messages to a queue, and receive and print messages with a listener. The receiver notifies and quits if the received message equals "quit".

Uploaded by

Atish Dabholkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Java

Messagi
Creating JMS Servers
ng
Services
 Similar Way Create JMS-2 with FS2 targeting to MS2

Note: If Health is not showing for JMS Servers targeted on managed server then you must apply
Patch 21830665 from oracle
Creating JMS System Module
Adding Resources to System_Module
Adding Connection Factory as Resource
Adding Queue as Resource
Adding Topic as Resource
Now run the below Program to Test
JmsUtil.java [select by ctrl+A and paste to eclipse]
package com.ktport;

import java.util.Hashtable;

import javax.jms.JMSException;

import javax.jms.QueueConnection;

import javax.jms.QueueConnectionFactory;

import javax.jms.QueueSession;

import javax.jms.Session;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.naming.NamingException;

public class JmsUtil {

public final static String QUEUE_JNDI_NAME = "jms/Queue";// Weblogic Queue JNDI

private static final String WEBLOGIC_JMS_URL = "t3://192.168.140.101:8001"; // Weblogic JMS URL

private final static String WEBLOGIC_JNDI_FACTORY_NAME = "weblogic.jndi.WLInitialContextFactory";

private final static String CONNECTION_FACTORY_JNDI_NAME = "jms/CF";// Weblogic ConnectionFactory JNDI

private static QueueConnection queueConnection = null;

private static QueueSession queueSession = null;

public static InitialContext getInitialContext()

throws NamingException

Hashtable<String, String> env = new Hashtable<String, String>();

env.put(Context.INITIAL_CONTEXT_FACTORY, WEBLOGIC_JNDI_FACTORY_NAME);// set Weblogic JNDI

env.put(Context.PROVIDER_URL, WEBLOGIC_JMS_URL);// set Weblogic JMS URL

env.put(Context.SECURITY_PRINCIPAL, "weblogic"); // set Weblogic UserName

env.put(Context.SECURITY_CREDENTIALS, "Oracle123"); // set Weblogic PassWord

InitialContext initialContext = new InitialContext(env);

return initialContext;

public static QueueSession getQueueSession(Context ctx) throws NamingException, JMSException {


JMSQueueMessageSender.Java
package com.ktport;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import javax.jms.JMSException;

import javax.jms.Queue;

import javax.jms.QueueSender;

import javax.jms.QueueSession;

import javax.jms.TextMessage;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.naming.NamingException;

import com.ktport.JmsUtil;

public class JMSQueueMessageSender

private QueueSender queueSender;

private Queue queue;

private TextMessage textMessage;

private void init(Context ctx, String queueName)throws NamingException, JMSException

QueueSession queueSession = JmsUtil.getQueueSession(ctx);

queue = (Queue) ctx.lookup(queueName);

queueSender = queueSession.createSender(queue);

textMessage = queueSession.createTextMessage();

private void send(String message) throws JMSException {

textMessage.setText(message);

queueSender.send(textMessage);

private void close() throws JMSException {

if(queueSender !=null)

queueSender.close();

JmsUtil.cleanUp();

private static void readAndSend(JMSQueueMessageSender jMSQueueMessageSender) throws IOException, JMSException


JMSQueueMessageReceiver.Java
package com.ktport;

import javax.jms.JMSException;

import javax.jms.Message;

import javax.jms.MessageListener;

import javax.jms.Queue;

import javax.jms.QueueReceiver;

import javax.jms.QueueSession;

import javax.jms.TextMessage;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.naming.NamingException;

import com.ktport.JmsUtil;

public class JMSQueueMessageReceiver implements MessageListener

private QueueReceiver queueReceiver;

private boolean quit = false;

@Override

public void onMessage(Message message)

try {

String jmsTextMessage;

if (message instanceof TextMessage) {

jmsTextMessage = ((TextMessage) message).getText();

} else {

jmsTextMessage = message.toString();

System.out.println("Message Received: " + jmsTextMessage);

if (jmsTextMessage.equalsIgnoreCase("quit")) {

synchronized (this) {

quit = true;

this.notifyAll(); // Notify main thread to quit

} catch (JMSException jmse) {

System.err.println("An exception occurred: " + jmse.getMessage());

You might also like