?? messageproducer.java
字號:
import javax.jms.*;
import javax.naming.*;
import java.util.*;
public class MessageProducer {
//We would like to keep a reference of TopicConnection,Session
//and TopicPublisher so that we can destroy it at the end.
private TopicConnection topicConnection = null;
private TopicPublisher topicPublisher = null;
private TopicSession topicSession = null;
//Names of machines over the network.
//These machines will have JMS Providers running.
//In real applications,names of machines should come
//from the GUI.
private final String COMPUTER1 = "computer6 ";
private final String COMPUTER2 = "computer13 ";
private final String COMPUTER3 = "computer14 ";
private final String COMPUTER4 = "computer15 ";
//Connect to the JMS Provider and send the message.
public String connectToProvider(String topicName, String jmsProvider,
String msg) {
//We will get a reference of System properties.
//We will add the Properties related to JMS functionality.
Properties env = System.getProperties();
env.put("com.sun.jms.internal.java.naming.factory.initial",
"com.sun.enterprise.naming.SerialInitContextFactory");
//Port number where the Naming service of
//remote JMS Provider is listening.
env.put("rg.omg.CORBA.ORBInitialPort ", "1050 ");
try {
//Set the network address of target machine
//where JMS Provider is running.
if (jmsProvider.equals("Server1 ")) {
env.put("org.omg.CORBA.ORBInitialHost", COMPUTER1);
} else if (jmsProvider.equals("Server2")) {
env.put("org.omg.CORBA.ORBInitialHost", COMPUTER2);
} else if (jmsProvider.equals("Server3")) {
env.put("org.omg.CORBA.ORBInitialHost", COMPUTER3);
} else if (jmsProvider.equals("Server4")) {
env.put("org.omg.CORBA.ORBInitialHost", COMPUTER4);
}
//Get an object of JNDI InitialContext
//from given JMS Provider
Context jndiContext = new InitialContext(env);
//Get JMS Administrator Objects
TopicConnectionFactory topicConnectionFactory =
(TopicConnectionFactory) jndiContext.lookup(
"TopicConnectionFactory");
Topic topic = (Topic) jndiContext.lookup(topicName);
//Establish TopicConnection and TopicSession.
topicConnection = topicConnectionFactory.createTopicConnection();
topicSession = topicConnection.createTopicSession(false,
Session.AUTO_ACKNOWLEDGE);
//Create the publisher for the topic of interest.
topicPublisher = topicSession.createPublisher(topic);
//Create Message Object and set the text.
TextMessage message = topicSession.createTextMessage();
message.setText(msg);
//We are all set,Send message.
topicPublisher.publish(message);
} catch (JMSException e) {
String err = new String("Exception occurred: " + e.toString());
return err;
} catch (NamingException e) {
String err = new String("Error in JNDI context= " + e.toString());
return err;
} finally {
if (topicConnection != null) {
try {
topicPublisher.close();
topicSession.close();
topicConnection.close();
} catch (JMSException e) {
return "Connection Closing Exception: " + e.toString();
}
} //if
} //finally
return "Message Sent Successfully";
} //connectToProvider()
} //end MessageProducer Class
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -