ENV setup for Point to Point:
Login the admin console of weblogic.
1.
http://localhost:7001/console
2.
Enter the username and
password(weblogic/weblogic)
3.
Create the JMS server. Servers-->messaging-->JMS
Server-->click on new button and enter the name of the server.
Select the targets as server name and click on finish.
4.
Go to the JMS Module under the Messaging
5. Click
on new then provide the name and click on next
6.
Select the target and click on finish
7.
Select the module hyper link and do the
following steps
Under the subdeployment tab click on new button
Provide the sub deployment name and click on next
Select the JMS server and click on finish
8.
Click on the module hyper link and click on new.
It will display the next page with the details. Here we need to create queue
and connection factory.
Select the connection factory radio button and click on next,
then provide the jndi name connection factory name.
Select the advanced targeting button. And select the target
and click on finish
9.
Create the queue for this click on next in the
module and select queue radio button and click on next it will display the
following screen.
Select the jndi name and clik on next.
Click on finish.
What is a message?
Message is a type of communication between s/w component and
application.
JMS Architecture:
1.
JMS Provider
2.
JMS client
3.
Message
4.
Administrated objects
5.
Native clients or programs
JMS Provider:
It is a messaging system that implements the JMS interfaces
and is provides the administrative and controlling features.
JMS client:
These are components written in java, consume message
Message:
These are objects that communicate b/w JMS clients
Administrated objects:
These are preconfigured JMS objects created by the administr
for the use of clients.
The JMS API
Programming
Model
·
Administrated objects
·
Connections
·
Sessions
·
Message producers
·
Message consumers
·
Message
JMS has two models
1.
Point to point queue
2.
Publish/subscribe topics
Writing
Simple JMS Client
Applications
·
Create a connection and session
·
Create producers and consumers
·
Sending and receiving a message
These applications are two types
1.
Point to point JMS model
2.
Publish/subscribe model
1.Writing a PTP program
To send a program
·
Create JNDI for QueueConnectionFactory and Queue
·
Create connection and message
·
Create a queue sender
·
Create text message
·
Send one or more messages to queue
·
Send a control message to indicate the end of
message stream
·
Close the connection in finally block
public class Send
{
public final static String JMS_FACTORY = "jndiConnectionFactory";
public final static String QUEUE = "QueueJndi";
public static final String URL = "t3://localhost:7001";
public static final String INITIAL_CONTEXT_FACTORY = "weblogic.jndi.WLInitialContextFactory";
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String[] args) throws NamingException, JMSException
{
//
1.Create JNDI for QueueConnectionFactory and Queue
Hashtable weblogicConfigs = new Hashtable(); weblogicConfigs.put(Context.INITIAL_CONTEXT_FACTORY,INITIAL_CONTEXT_FACT ORY);
weblogicConfigs.put(Context.PROVIDER_URL, URL);
weblogicConfigs.put(Context.SECURITY_CREDENTIALS, "weblogic");
weblogicConfigs.put(Context.SECURITY_PRINCIPAL, "weblogic");
Context context = new InitialContext(weblogicConfigs);
QueueConnectionFactory qconFactory
= (QueueConnectionFactory) context.lookup(JMS_FACTORY);
Queue queue = (Queue) context.lookup(QUEUE);
//2.Create
connection and message
QueueConnection qcon = qconFactory.createQueueConnection();
QueueSession qsession =
qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
QueueSender qsender =
qsession.createSender(queue);
TextMessage msg =
qsession.createTextMessage();
qcon.start();
msg.setText("Welcome to JMS");
qsender.send(msg);
//close the
connections
qsender.close();
qsession.close();
qcon.close();
}
}
To receive a program
·
JNDI look up for the QueueConnectionFactory and
Queue
·
Create a connection and session
·
Create a queue receiver
·
Start the connection, causing to message
delivery begin
·
Receives the messages until the
end-of-message-stream-control message is received
·
Close the connection
public class
Receive implements MessageListener
{
public
final static
String JMS_FACTORY = "jndiConnectionFactory";
public
final static
String QUEUE = "QueueJndi";
public
static final
String URL = "t3://localhost:7001";
public
static final
String INITIAL_CONTEXT_FACTORY = "weblogic.jndi.WLInitialContextFactory";
@SuppressWarnings({
"rawtypes", "unchecked" })
public
static void
main(String[] args) throws
NamingException, JMSException
{
// 1.JNDI look up for the
QueueConnectionFactory and Queue
Hashtable
weblogicConfigs = new Hashtable();
weblogicConfigs.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
weblogicConfigs.put(Context.PROVIDER_URL, URL);
weblogicConfigs.put(Context.SECURITY_CREDENTIALS, "weblogic");
weblogicConfigs.put(Context.SECURITY_PRINCIPAL, "weblogic");
Context context
= new
InitialContext(weblogicConfigs);
QueueConnectionFactory
qconFactory = (QueueConnectionFactory) context.lookup(JMS_FACTORY);
Queue queue =
(Queue) context.lookup(QUEUE);
// 2.Create a connection and session
QueueConnection
qcon = qconFactory.createQueueConnection();
QueueSession
qsession = qcon.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
// 3.Create
a queue receiver
QueueReceiver
qreceiver = qsession.createReceiver(queue);
//4.Start
the connection, causing to message delivery begin
qreceiver.setMessageListener(this);
qcon.start();
//6.Close
the connection
qreceiver.close();
qsession.close();
qcon.close();
}
//5.Receives
the messages until the end-of-message-stream-control message is received
public
void onMessage(Message msg)
{
String msgText =
"";
if (msg instanceof
TextMessage)
{
try
{
msgText
= ((TextMessage) msg).getText();
}
catch (JMSException e)
{
e.printStackTrace();
}
} else
{
msgText
= msg.toString();
}
System.out.println("Message
Received: " + msgText);
}
}
2. publish/subscribe model
To send a message:
·
Perform JNDI API lookup for the
TopicConnectionFactory and Topic
·
Create the connection and session
·
Create a TopicPublisher
·
Create a text message
·
Publishes one or more messages to the topic
·
Close the connection
public class SimpleTopicPublisher
{
public final static String JMS_FACTORY = "jndiConnectionFactory";
public final static String QUEUE = "QueueJndi";
public static final String URL = "t3://localhost:7001";
public static final String INITIAL_CONTEXT_FACTORY = "weblogic.jndi.WLInitialContextFactory";
/**
* @param args
* @throws NamingException
* @throws JMSException
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void main(String[] args) throws NamingException, JMSException
{
Hashtable weblogicConfigs = new Hashtable();
weblogicConfigs.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
weblogicConfigs.put(Context.PROVIDER_URL, URL);
weblogicConfigs.put(Context.SECURITY_CREDENTIALS, "weblogic");
weblogicConfigs.put(Context.SECURITY_PRINCIPAL, "weblogic");
Context ctx = new InitialContext(weblogicConfigs);
Topic topic = (Topic) ctx.lookup("topic");
TopicConnectionFactory factory =
(TopicConnectionFactory) PortableRemoteObject.narrow(ctx.lookup("jndiConnectionFactory"),
TopicConnectionFactory.class);
TopicConnection connection =
factory.createTopicConnection();
TopicSession session =
connection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
TextMessage message =
session.createTextMessage("Welcome to MDB");
TopicPublisher pub =
session.createPublisher(topic);
pub.send(message);
}
}
To receive a message:
·
Perform the JNDI lookup for
TopicConnecctionFactory and Topic
·
Create a connection and session
·
Create a TopicPublisher
·
Create an instance of TextListener class and
register it as a message listener for the TopicSubscriber
·
Start the connection and consuming the message
delivery to begin
·
Listeners for message published to the topic,
stopping when user enter the chars 'q/Q'
·
Close the connection
No comments:
Post a Comment