...
new Thread(new Runnable() {
public void run() {
// retrieve DB data
try{ Thread.sleep(10000);
}catch(InterruptedException e){}
}).start();
new Thread(new Runnable() {
public void run() {
// retrieve DB data
try{ Thread.sleep(10000);
}catch(InterruptedException e){}
}).start();
我觉得楼上说的效率确实是应该考虑的问题
把你的帖子的链接贴上来或者再贴一遍代码了,谢谢!
package commonclass.msgs;import java.util.*;
import javax.jms.*;
import javax.naming.*;import commonclass.*;public class TopicBase {
public final static String JNDI_FACTORY =
"weblogic.jndi.WLInitialContextFactory";
public String FactoryName = "";
public String TopicName = "";
public InitialContext ctx = null;
public TopicConnectionFactory TCF = null;
public Topic topic = null;
public TopicConnection TC = null;
public TopicSession TSession = null; public TopicBase() {
getTopicInit();
} private void getTopicInit() {
PropertyFile pFile = new PropertyFile();
pFile.setPropFile("c:/webinfo/web.properties");
FactoryName = pFile.getProp("CFName");
TopicName = pFile.getProp("TopicName");
try {
ctx = getInitialContext();
TCF = (TopicConnectionFactory) ctx.lookup(FactoryName);
topic = (Topic) ctx.lookup(TopicName);
ctx.close();
TC = TCF.createTopicConnection();
}
catch (Exception e) {
e.printStackTrace();
}
} private static InitialContext getInitialContext() throws NamingException {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
return new InitialContext(env);
} public void Start() {
try {
TC.start();
}
catch (Exception e) {
e.printStackTrace();
}
} public void Stop() {
try {
TC.stop();
}
catch (Exception e) {
e.printStackTrace();
}
} public void Close() {
try {
TSession.close();
TC.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}///////////////////////////
发送程序 在你的改动数据库的程序里创建他的实例
调用public void publish(String msgStr) msgStr就是消息的内容
//////////////////////////
package commonclass.msgs;import javax.jms.*;public class Publisher
extends TopicBase {
private TopicPublisher TPublisher = null;
private TextMessage TMSG = null;
private String MsgContent = "no message set";
public Publisher() {
super();
try {
TSession = TC.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
TPublisher = TSession.createPublisher(topic);
}
catch (Exception e) {
e.printStackTrace();
}
} private void setMessage(String msg) {
MsgContent = msg;
} public void publish(String msgStr) {
setMessage(msgStr);
try {
TMSG = TSession.createTextMessage(MsgContent);
TPublisher.publish(TMSG);
}
catch (Exception e) {
e.printStackTrace();
}
} public void closePublisher() {
try {
TPublisher.close();
Close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
//////////////////
接收 在onMessage(Message message)里改写你的代码 或者继承他重载这个方法
但是你要保证这个对象的生命周期。保证他在整个web页面被浏览的时候都存在如放在applet里等
其他方法我还没找到 有更好的方法请告诉我 [email protected]
//////////////////
package commonclass.msgs;import javax.jms.*;public class Subscriber
extends TopicBase
implements MessageListener {
protected boolean ifDurable = false;
protected TopicSubscriber TSubscriber = null;
protected static String clientID; public Subscriber() {
super();
try {
TSession = TC.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
TSubscriber = TSession.createSubscriber(topic);
TSubscriber.setMessageListener(this);
}
catch (Exception e) {
e.printStackTrace();
}
} public Subscriber(String clientid) {
super();
ifDurable = true;
clientID = clientid;
try {
TC.setClientID(clientID);
TSession = TC.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
TSubscriber = TSession.createDurableSubscriber(topic, clientID);
TSubscriber.setMessageListener(this);
}
catch (Exception e) {
e.printStackTrace();
}
} public void onMessage(Message message) {
String msgText = "";
String msgID = "";
try {
msgID = message.getJMSMessageID();
if (message instanceof TextMessage) {
msgText = ( (TextMessage) message).getText();
}
else {
msgText = message.toString();
}
}
catch (JMSException jmse) {
jmse.printStackTrace();
}
System.out.println("There is a Message received:");
System.out.println(msgText);
} public void closeSubscriber() {
try {
TSubscriber.close();
Close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}嘿嘿刚写出来还没测试 不过估计没什么大问题
最近刚研究jms欢迎大家和我交流[email protected]
http://java.sun.com/products/jms/tutorial/1_3_1-fcs/doc/copyright.html