📄 serverreceive.java
字号:
//声明这个类所在的包为examples.jms.startup
package examples.jms.startup;
//声明这个类引入的其它类 和包
import java.io.*;
import java.util.*;
import javax.transaction.*;
import javax.naming.*;
import javax.jms.*;
import weblogic.common.*;
/**
* 这个实例建立一个消息监听器,当消息从主题接收,它们显示在命令行并写入weblogic.log文件。
* 可以用TopicSend客户端发送消息。
*/
public class ServerReceive
implements MessageListener, T3StartupDef
{
//声明t3服务对象
private T3ServicesDef services;
//声明主题连接构造器
private TopicConnectionFactory tconFactory;
//声明主题连接
private TopicConnection tcon;
//声明主题会话
private TopicSession tsession;
//声明主题订阅
private TopicSubscriber tsubscriber;
//声明主题
private Topic topic;
/**
* 设置t3服务对象
*
* @参数 services Services stub.
*/
public void setServices(T3ServicesDef services) {
this.services = services;
}
/**
* 创建使用者
* @参数 name 名字
* @参数 args Hashtable.
* @异常 Exception if problems occurs
*/
public String startup(String name, Hashtable args)
throws Exception
{
//Thread.sleep(120000);
//从参数哈希表中获取连接构造器
String connectionFactory = (String)args.get("connectionFactory");
//从参数哈希表中获取主题名
String topicName = (String)args.get("topic");
if (connectionFactory == null || topicName == null) {
//抛出异常
throw new IllegalArgumentException("connectionFactory="+connectionFactory+
", topicName="+topicName);
}
//实例上下文
Context ctx = new InitialContext();
//通过上下文查找连接构造器
tconFactory = (TopicConnectionFactory) ctx.lookup(connectionFactory);
//创建主题连接
tcon = tconFactory.createTopicConnection();
//创建主题会话
tsession = tcon.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
//查找主题
topic = (Topic) ctx.lookup(topicName);
//创建消息订阅者
tsubscriber = tsession.createSubscriber(topic);
//设置消息监听器
tsubscriber.setMessageListener(this);
//启动连接
tcon.start();
return "Ready To Receive Messages";
}
/** 实现消息监听接口MessageListener定义的方法
* @参数 msg 消息
*/
public void onMessage(Message msg)
{
try {
//消息文本
String msgText = ((TextMessage)msg).getText();
//日志记录
services.log().info("JMS Message Received: "+ msgText);
System.out.println("JMS Message Received: "+ msgText);
} catch (JMSException jmse) {
jmse.printStackTrace();
} catch (T3Exception t3e) {
t3e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -