📄 topicsend.java
字号:
package com.test;
import java.io.*;
import java.util.*;
import javax.transaction.*;
import javax.naming.*;
import javax.jms.*;
public class TopicSend
{
public final static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";
public final static String JMS_FACTORY = "weblogic.examples.jms.TopicConnectionFactory";
public final static String TOPIC = "weblogic.examples.jms.exampleTopic";
private TopicConnectionFactory tconFactory;
private TopicConnection tcon;
private TopicSession tsession;
private TopicPublisher tpublisher;
private Topic topic;
private TextMessage msg;
public void init(Context ctx,String topicName) throws NamingException,JMSException
{
tconFactory = (TopicConnectionFactory)ctx.lookup(JMS_FACTORY);
tcon = tconFactory.createTopicConnection();
tsession = tcon.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);
topic = (Topic)ctx.lookup(topicName);
tpublisher = tsession.createPublisher(topic);
msg = tsession.createTextMessage();
tcon.start();
}
public void send(String message) throws JMSException
{
msg.setText(message);
tpublisher.publish(msg);
}
public void close() throws JMSException
{
tpublisher.close();
tsession.close();
tcon.close();
}
public static void main(String args[]) throws Exception
{
if(args.length!=1)
{
System.out.println("Usage:java examjples.jms.topic.TopicSend WebLogicURL");
return;
}
InitialContext ic = getInitialContext(args[0]);
TopicSend ts = new TopicSend();
ts.init(ic,TOPIC);
readAndSend(ts);
ts.close();
}
private static void readAndSend(TopicSend ts) throws IOException,JMSException
{
BufferedReader msgStream = new BufferedReader(new InputStreamReader(System.in));
String line = null;
boolean quitNow = false;
do
{
System.out.print("Enter message(\"quit\" to quit):");
line = msgStream.readLine();
if(line!=null&&line.trim().length()!=0)
{
ts.send(line);
System.out.println("JMS Message Send:"+line+"\n");
quitNow = line.equalsIgnoreCase("quit");
}
}while(!quitNow);
}
private static InitialContext getInitialContext(String url) throws NamingException
{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,JNDI_FACTORY);
env.put(Context.PROVIDER_URL,url);
return new InitialContext(env);
}
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -