public static String url = “file:/D:/JNDI-Directory01”;
public static void main(String[] vars) throws JMSException, NamingException
{
ConnectionFactory factory = null;
Connection connection = null;
Session session = null;
Destination destination= null; // a destination can be a topic or a queue
MessageProducer producer= null;
try
{
JNDIUtil jndiUtil= new JNDIUtil(icf,url);
factory= jndiUtil.getConnectionFactory(“TestQMConnectionFactory”);
connection = factory.createConnection();
connection.start();
//您可以使用连接对象来创建一个会话:
// Indicate a non-transactional session
boolean transacted = false;
session = connection.createSession( transacted, Session.AUTO_ACKNOWLEDGE);
//发布消息,获取新闻的目标对象,创建一个MessageProducer,然后发送消息:
destination = jndiUtil.getDestination(“NewsTopic”);
producer = session.createProducer(destination);
TextMessage message = session.createTextMessage(“No News is Good News!111”);
producer.send(message);
System.out.println(“NewsPublisher: Message Publication Completed”);
}
finally
{
// Always release resources
if ( producer!= null )
producer.close();
if ( session!= null )
session.close();
if ( connection!= null )
connection.close();
}
}
}
EmailSubscriber和SMSSubscriber
这两个类的电子邮件应用程序和SMS应用程序(参见清单3和清单4)。因为你已经定义在MQ资源管理器中订阅,这两个应用程序简单地从用户队列中读取消息。
Java的代码
package com.test.pubsub;
/**
*
Listing
3.
EmailSubscriber
*
This
class
represents
the
email
application.
*/
//JMS classes
import javax.jms.JMSException;
import javax.jms.ConnectionFactory;
import javax.jms.Connection;
import javax.jms.Session;
import javax.jms.Destination;
import javax.jms.MessageConsumer;
import javax.jms.TextMessage;
//JNDI classes
import javax.naming.NamingException;
/**
*
A
class
to
demonstrate
how
to
retrieve
publications
from
subscription
queues.
*
EmailSubscriber和SMSSubscriber
*
这两个类的电子邮件应用程序和SMS应用程序(参见清单3和清单4)。
*
因为你已经定义在MQ资源管理器中订阅,这两个应用程序简单地从用户队列中读取消息。
*/
public
class EmailSubscriber
{
public
static String icf = “com.sun.jndi.fscontext.RefFSContextFactory”;
public
static String url = “file:/D:/JNDI-Directory01”;
public
static
void main(String[] vars) throws JMSException, NamingException
{
ConnectionFactory factory = null;
Connection connection = null;
Session session = null;
Destination destination= null; // a destination can be a topic or a queue
MessageConsumer consumer= null;
try
{
JNDIUtil jndiUtil= new JNDIUtil(icf,url);
factory= jndiUtil.getConnectionFactory(“TestQMConnectionFactory”);
connection = factory.createConnection();
connection.start();
// Indicate a non-transactional session
boolean transacted = false;
session = connection.createSession( transacted, Session.AUTO_ACKNOWLEDGE);
destination = jndiUtil.getDestination(“EmailQueue”);
consumer = session.createConsumer(destination);
TextMessage iMsg = (TextMessage) consumer.receive(1000);
if ( iMsg != null )
System.out.println( iMsg.getText() );
else
System.out.println( “No messages in queue “ );
System.out.println(“EmailSubscriber: Message Reading Completed”);
}
finally
{
// Always release resources
if ( consumer!= null )
consumer.close();
if ( session!= null )
session.close();
if ( connection!= null )
connection.close();
}
}
}
package com.test.pubsub;
/**
*
Listing
4.
SMSSubscriber
*
This
class
represents
the
SMS
application.
*/
//JMS classes
import javax.jms.JMSException;
import javax.jms.ConnectionFactory;
import javax.jms.Connection;
import javax.jms.Session;
import javax.jms.Destination;
import javax.jms.MessageConsumer;
import javax.jms.TextMessage;
//JNDI classes
import javax.naming.NamingException;
/**
*
A
class
to
demonstrate
how
to
retrieve
publications
from
subscription
queues.
*/
public
class SMSSubscriber
{
public
static String icf = “com.sun.jndi.fscontext.RefFSContextFactory”;
public
static String url = “file:/D:/JNDI-Directory01”;
public
static
void main(String[] vars) throws JMSException, NamingException
{
ConnectionFactory factory = null;
Connection connection = null;
Session session = null;
Destination destination= null; // a destination can be a topic or a queue
MessageConsumer consumer= null;
try
{
JNDIUtil jndiUtil= new JNDIUtil(icf,url);
factory= jndiUtil.getConnectionFactory(“TestQMConnectionFactory”);
connection = factory.createConnection();
connection.start();
// Indicate a non-transactional session
boolean transacted = false;
session = connection.createSession( transacted, Session.AUTO_ACKNOWLEDGE);
destination = jndiUtil.getDestination(“SMSQueue”);
consumer = session.createConsumer(destination);
TextMessage iMsg = (TextMessage) consumer.receive(1000);
if ( iMsg != null )
System.out.println( iMsg.getText() );
else
System.out.println( “No messages in queue “ );
以下文章点击率最高
Loading…