📄 messageproducter.java
字号:
/*******************************************************************
<br>Copyright (C), 2004-2006, yeeku.H.Lee
<br>Program Name:SyncConsumer.java
<br>作用:演示pub/sub消息生产者程序。
<br>Author:yeeku.H.lee kongyeeku@163.com
<br>Version:1.0
<br>This program is protected by copyright laws.
<br>Date: 2005-9-22
*******************************************************************/
package leejms;
import java.io.PrintStream;
import java.util.Properties;
import javax.jms.*;
import javax.naming.*;
public final class MessageProducter
{
private TopicPublisher publisher;
private TextMessage msg;
public MessageProducter()throws NamingException, JMSException
{
Context context = getInitialContext();
TopicConnectionFactory topicconnectionfactory = (TopicConnectionFactory)context.lookup("weblogic.jms.ConnectionFactory");
Topic topic = (Topic)context.lookup("MessageTopic");
TopicConnection topicconnection = topicconnectionfactory.createTopicConnection();
TopicSession topicsession = topicconnection.createTopicSession(false/*非事物性会话*/,Session.AUTO_ACKNOWLEDGE);
publisher = topicsession.createPublisher(topic);
msg = topicsession.createTextMessage();
}
public void publishMessage()throws JMSException
{
msg.setText("Hello J2EE");
publisher.publish(msg);
msg.setText("Welcome to JMS Publisher Message");
publisher.publish(msg);
}
//----------------公用函数,用来获取命名服务的Context对象-------------------------
protected Context getInitialContext()
{
String s = "t3://localhost:8181";
Object obj = null;
InitialContext initialcontext = null;
try
{
Properties properties = new Properties();
properties.put("java.naming.factory.initial", "weblogic.jndi.WLInitialContextFactory");
properties.put("java.naming.provider.url", s);
initialcontext = new InitialContext(properties);
}
catch(NamingException namingexception)
{
System.err.println("不能连到WebLogic server在" + s);
namingexception.printStackTrace();
}
return initialcontext;
}
//------------------------主函数,程序的入口-----------------------------
public static void main(String args[])throws Exception
{
MessageProducter messageproducter = new MessageProducter();
messageproducter.publishMessage();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -