java带过滤功能的新闻服务提供系统代码
主要是使用Java JMS 提供的Pub/Sub 服务可以实现许多功能,诸如聊天室、ICQ 以及各种订约系统,本实例利用Pub/Sub 服务和消息驱动Bean 实现了新闻服务提供系统的原型,并且加上了过滤系统。实现方法主要是利用JMS 提供的广播机制,构建一个Publisher Bean 负责向消息主题发送消息,一个消息驱动Bean 负责接受消息,一个客户端向Publisher 发出发送消息的命令。程序代码如下:
编写PublisherHome 接口,该接口继承了EJBHome 接口,客户端用它来创建Publisher Bean实例,代码如下:
import java.rmi.RemoteException;
import javax.ejb.EJBHome;
import javax.ejb.CreateException;
/**
* EJB Publisher 的Home 接口
*/
public interface PublisherHome extends EJBHome
{
Publisher create() throws RemoteException, CreateException;
}
编写Publisher 接口,该接口继承了EJBObject 接口,为Publisher Session Bean 的Remote接口,客户端用它来发出发布新闻的命令:
import javax.ejb.*;
import java.rmi.RemoteException;
/**
* EJB Publisher 的Remote 接口
*/
public interface Publisher extends EJBObject
{
void publishNews() throws RemoteException;
}
编写PublisherBean 类,该类实现了SessionBean 接口,Publisher Bean 在ejbCreate()方法中创建链接,初始化资源,在ejbRemove()方法中释放资源:
import java.rmi.RemoteException;
import java.util.*;
import javax.ejb.*;
import javax.naming.*;
import javax.jms.*;
/**
* EJB Publisher 的具体实现
* 定义了publishNews 方法
*/
public class PublisherBean implements SessionBean
{
SessionContext sc = null;
TopicConnection topicConnection = null;
Topic topic = null;
// 定义消息类型
final static String messageTypes[] = {"Nation/World",
"Metro/Region", "Business", "Sports", "Living/Arts",
"Opinion"};
public PublisherBean()
{
System.out.println("In PublisherBean() (constructor)");
}
/**
* 设置相关的Session 上下文环境。
* EJB 容器在创建此类实例后调用它
*/
public void setSessionContext(SessionContext sc)
{
this.sc = sc;
}
/**
* 初始化EJB.
* 创建TopicConnection 对象,并且查找相关Topic
*/
public void ejbCreate()
{
Context context = null;
TopicConnectionFactory topicConnectionFactory = null;
System.out.println("In PublisherBean.ejbCreate()");
try
{
// 初始化JNDI
context = new InitialContext();
// 查找消息主题对象
topic = (Topic)context.lookup("java:comp/env/jms/TopicName");
// 创建一个TopicConnection 对象
topicConnectionFactory = (TopicConnectionFactory)
context.lookup("java:comp/env/jms/MyTopicConnectionFactory");
topicConnection =
topicConnectionFactory.createTopicConnection();
}
catch (Throwable t)
{
// 返回JMSException 或者 NamingException
System.err.println("PublisherBean.ejbCreate:" +
"Exception: " + t.toString());
}
}
/**
* publishNews ()调用它
* 利用随机数生成器来选择一个消息
*/
private String chooseType()
{
int whichMsg;
Random rgen = new Random();
whichMsg = rgen.nextInt(messageTypes.length);
return messageTypes[whichMsg];
}
/**
* 创建TopicSession、消息发送者以及消息对象,
* 并且发布某种消息。消息由message-driven bean
* 通过消息选择器来接受
/
public void publishNews() throws EJBException
{
TopicSession topicSession = null;
TopicPublisher topicPublisher = null;
TextMessage message = null;
int numMsgs = messageTypes.length * 3;
String messageType = null;
try
{
topicSession =
topicConnection.createTopicSession(true, 0);
topicPublisher = topicSession.createPublisher(topic);
message = topicSession.createTextMessage();
for (int i = 0; i < numMsgs; i++) {
messageType = chooseType();
message.setStringProperty("NewsType",
messageType);
message.setText("Item " + i + ": " +
messageType);
System.out.println("PUBLISHER: Setting " +
"message text to: " + message.getText());
topicPublisher.publish(message);
}
}
catch (Throwable t)
{
/ 可能产生JMSException
System.err.println("PublisherBean.publishNews: " +
"Exception: " + t.toString());
// 如果抛出异常就回滚事务
sc.setRollbackOnly();
{
finally
{
if (topicSession != null)
{
try
{
// 关闭打开的会话
topicSession.close();
} catch (JMSException e)
{
}
}
}
}
/**
* 在MDB 删除时,关闭TopicConnection
*/
public void ejbRemove() throws RemoteException
{
System.out.println("In PublisherBean.ejbRemove()");
if (topicConnection != null)
{
try
{
topicConnection.close();
}
catch (Exception e)
{
e.printStackTrace();
{
}
}
public void ejbActivate() {}
public void ejbPassivate() {}
}
编写MessageBean 类,该类实现了MessageDrivenBean 类,EJB 容器在创建这个MDB 的实例时,首先初始化一个实例,然后调用setMessageDrivenContext 方法把这个实例和一个MessageDrivenContext 对象关联起来。它的onMessage 方法首先判断一下消息的类型,如果是TextMessage 类型就打印出消息的内容,代码如下:
import javax.ejb.*;
import javax.naming.*;
import javax.jms.*;
/**
* MessageBean 类是消息驱动Bean
* MessageBean 类实现了javax.ejb.MessageDrivenBean 和javax.jms.MessageListener 这两个接口
*/
public class MessageBean implements MessageDrivenBean,
MessageListener
{
private transient MessageDrivenContext mdc = null;
private Context context;
public MessageBean()
{
System.out.println("In MessageBean.MessageBean()");
}
/**
* setMessageDrivenContext 方法,
* 这个实例和一个MessageDrivenContext 对象关联起来
*/
public void setMessageDrivenContext(MessageDrivenContext mdc)
{
System.out.println("In " +
"MessageBean.setMessageDrivenContext()");
this.mdc = mdc;
}
/**
ejbCreate 方法
*/
public void ejbCreate()
{
System.out.println("In MessageBean.ejbCreate()");
}
/**
* onMessage 方法,
* 首先判断一下消息的类型,如果是TextMessage 类型就打印出消息的内容
*/
public void onMessage(Message inMessage)
{
TextMessage msg = null;
try {
// 看看是否是TextMessage 消息
if (inMessage instanceof TextMessage) {
msg = (TextMessage) inMessage;
System.out.println("MESSAGE BEAN: Message " +
"received: " + msg.getText());
} else {
System.out.println("Message of wrong type: " +
inMessage.getClass().getName());
}
} catch (JMSException e) {
System.err.println("MessageBean.onMessage: " +
"JMSException: " + e.toString());
mdc.setRollbackOnly();
} catch (Throwable te) {
System.err.println("MessageBean.onMessage: " +
"Exception: " + te.toString());
}
}
public void ejbRemove() {
System.out.println("In MessageBean.remove()");
}
}
编写MyappClient 类,该类仅包括main()方法,在该方法中客户端程序首先使用JNDI 查找Publisher Bean 的Home 对象,然后调用Home 对象创建Publihser 的EJB 对象实例,并且调用publisherNews 方法指向Publisher Bean 发出发布新闻消息的命令,代码如下:
view sourceprint?01import javax.ejb.EJBHome;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;
import javax.jms.*;
public class MyAppClient
{
public static void main (String[] args)
{
MyAppClient client = new MyAppClient();
client.doTest();
System.exit(0);
}
public void doTest()
{
try {
// 初始化JNDI
Context ic = new InitialContext();
System.out.println("Looking up EJB reference");
// 获取Home 对象的引用,这是EJB 对象的工厂
java.lang.Object objref =
ic.lookup("java:comp/env/ejb/MyEjbReference");
System.err.println("Looked up home");
PublisherHome pubHome = (PublisherHome)
PortableRemoteObject.narrow(objref,
PublisherHome.class);
System.err.println("Narrowed home");
/*
* 创建Bean 实例, 调用其商业方法publishNews 两次,Remove 此Bean 实例
*/
Publisher phr = pubHome.create();
System.err.println("Got the EJB");
phr.publishNews();
phr.publishNews();
phr.remove();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
¥29.00
¥299.00
¥399.00
¥498.00