Java 定时器之commonj timer,jms & mdb驱动,servlet 初始化:
Business:
jms:
mdb:
Business:
package com.wxs.simple.ejb;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.naming.InitialContext;
import com.wx.simple.listener.ScheduleTaskListener;
import commonj.timers.Timer;
import commonj.timers.TimerListener;
import commonj.timers.TimerManager;
@Stateless(name = "ScheduleBean_stateless", mappedName = "ScheduleBean")
@Remote(ScheduleBeanRemote.class)
public class ScheduleBean implements ScheduleBeanRemote {
Timer timer = null;
@PostConstruct
public void init() {
System.out
.println("service() entering try block to intialize the timer from JNDI");
try {
System.out.println("In ScheduleBean -> scheduleService method");
InitialContext ic = new InitialContext();
TimerManager jobScheduler = (TimerManager) ic
.lookup("weblogic.JobScheduler");
System.out.println("jobScheduler reference " + jobScheduler);
TimerListener timerListener = new ScheduleTaskListener();
System.out.println("timerListener reference " + timerListener);
timer = jobScheduler.schedule(timerListener, 0, 30 * 1000);
// execute this job every 5 seconds
System.out.println("service() started the timer");
} catch (Throwable t) {
System.out
.println("service() error initializing timer manager with JNDI name weblogic.JobScheduler "
+ t);
}
}
@PreDestroy
public void clearTimer(){
System.out.println("Begin stop timer!");
timer.cancel();
System.out.println("End stop timer!");
}
public void scheduleService() {
System.out
.println("In ScheduleBean ->scheduleService method, do Task... ");
}
}
jms:
package com.wx.simple.listener;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import commonj.timers.Timer;
import commonj.timers.TimerListener;
public class ScheduleTaskListener implements Serializable, TimerListener {
private static final long serialVersionUID = 8313912206357147939L;
public void timerExpired(Timer timer) {
SimpleDateFormat sdf = new SimpleDateFormat();
System.out.println("In ScheduleTaskListener-> timerExpired method"
+ sdf.format(new Date()));
try {
InitialContext iniCtx = new InitialContext(System.getProperties());
TopicConnectionFactory factory = (TopicConnectionFactory) iniCtx
.lookup("javax.jms.TopicConnectionFactory");
TopicConnection con = factory.createTopicConnection();
TopicSession session = con.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = (Topic)iniCtx.lookup("jms/Topic");
TopicPublisher pub = session.createPublisher(topic);
TextMessage tmsg = session.createTextMessage();
tmsg.setText("Schedule for batch distribution...");
System.out.println("Begin send message...");
pub.send(tmsg);
System.out.println("End send message....");
pub.close();
System.out.println("End ScheduleTaskListener-> timerExpired method");
} catch (NamingException e) {
e.printStackTrace();
} catch (JMSException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
mdb:
package com.wx.simple.ejb;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.Message;
import javax.jms.MessageListener;
@MessageDriven(activationConfig = {
@ActivationConfigProperty(propertyName="destinationType",
propertyValue="javax.jms.Topic")
})
public class DoBatchBusiness implements MessageListener{
public void onMessage(Message msg) {
System.out.println("In method onMessage");
if (null != msg) {
System.out.println("received message from schedule task listener:"+msg);
}
}
}