【Java开源代码栏目提醒】:网学会员--在 Java开源代码编辑为广大网友搜集整理了:TopicSupplier.java绩等信息,祝愿广大网友取得需要的信息,参考学习。
package ejava.messagech22.jms;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;
import java.util.Properties;
import java.util.Hashtable;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.jms.TopicPublisher;
import javax.jms.Topic;
import javax.jms.TextMessage;
import javax.jms.ObjectMessage;
import javax.jms.JMSException;
import javax.jms.Session;
import java.io.Serializable;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.Hashtable;
import java.util.Enumeration;
import java.sql.SQLException;
/**
* This class gets orders from the database and
* submits new orders on a given topic to the consumer.
*/
public class TopicSupplier
{
private TopicConnectionFactory topicConnectionFactory;
private TopicConnection topicConnection;
private TopicSession topicSession;
private TopicPublisher topicPublisher;
private Topic topic;
private Properties properties;
public TopicSupplier(Properties defaultProperties,
Context context)
throws NamingException, JMSException
{
properties = defaultProperties;
String jmsFactoryName = (String)properties.get("JMS_FACTORY_FOR_TOPIC");
// get TopicFactory
topicConnectionFactory = (TopicConnectionFactory)
context.lookup(jmsFactoryName);
// Get TopicConnection
topicConnection = topicConnectionFactory.createTopicConnection();
// Get Session for the Connection
topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
String topicName = (String)properties.get("TOPIC_NAME");
try {
// determine if topic by this name already exists
topic = (Topic) context.lookup(topicName);
} catch (NamingException namingException) {
// if not there, create new topic
topic = topicSession.createTopic(topicName);
// bind the topic with a name
context.bind(topicName, topic);
}
topicPublisher = topicSession.createPublisher(topic);
}
/**
* Send a message to a Topic.
*/
public void send(Object newMessage)
throws JMSException
{
if(newMessage instanceof String){
TextMessage sendingMessage = topicSession.createTextMessage();
topicConnection.start(); // start Sending
sendingMessage.setText((String)newMessage); // set the Text message
topicPublisher.publish(sendingMessage); // send Message
}else {
ObjectMessage sendingMessage = topicSession.createObjectMessage();
topicConnection.start(); // start Sending
sendingMessage.setObject((OrderRequest)newMessage); // set Object Message
topicPublisher.publish(sendingMessage); // send message
}
}
/**
* Close JMS Reletated Objects.
*/
public void close()
throws JMSException
{
topicPublisher.close();
topicSession.close();
topicConnection.close();
}
/**
* it returns the initial Context to the Topic Service
*/
private static InitialContext getInitialContext(Properties properties)
throws NamingException
{
Hashtable env = new Hashtable();
String jndiFactory = (String)properties.get("JNDI_FACTORY");
String providerURL = (String)properties.get("JNDI_PROVIDER_URL");
env.put(Context.INITIAL_CONTEXT_FACTORY, jndiFactory);
env.put(Context.PROVIDER_URL, providerURL);
return new InitialContext(env);
}
/**
* Create a new Topic Publisher and publish the Order to that Topic
*/
public static void createATopicAndPublishOrder(Properties properties,
OrderRequest orderRequest)
{
try{
InitialContext context = getInitialContext(properties);
TopicSupplier topicSupplier =
new TopicSupplier(properties, context);
topicSupplier.send(orderRequest);
topicSupplier.close();
context.close();
}
catch(JMSException jmsException){
System.out.println("Error :"+jmsException);
jmsException.printStackTrace();
}
catch(NamingException namingException){
System.out.println("Error :"+namingException);
namingException.printStackTrace();
}
}
public static void main(String[] args)
throws Exception
{
if (args.length != 1) {
System.out.println("Usage: java ejava.messagech22.TopicSupplier"+
" jmsch22.Properties");
return;
}
Properties properties = new Properties();
String fileName = args[0];
try{
// Read properties file info
FileInputStream fin = new FileInputStream(fil