Commonj Timer 使用

Java 定时器之commonj timer,jms & mdb驱动,servlet 初始化:

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);
}
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这段SQL语句的作用是根据种植区域ID查询种植信息,并联合查询了`zhnl_crops`表获取作物名称和品种。 具体解释如下: - `<select>`:表示这是一个查询语句。 - `id="queryAreaCropsByAreaId"`:定义了这个查询语句的唯一标识符,可以在其他地方引用。 - `parameterClass="commonj.sdo.DataObject"`:指定了查询语句的参数类型为`commonj.sdo.DataObject`,这是一种通用的数据对象类型。 - `resultClass="commonj.sdo.DataObject"`:指定了查询结果的类型为`commonj.sdo.DataObject`,同样是一种通用的数据对象类型。 接下来是具体的SQL查询语句: ```sql select t.*,c.cropsname,c.varieties from zhnl_area_crops t LEFT JOIN zhnl_crops c on t.cropsid = c.id where 1=1 and t.areasid= #areasid# and t.status = '1' ``` 解释每个部分的含义: - `select`:表示要选择的列。 - `t.*, c.cropsname, c.varieties`:表示选择`t`表中的所有列,以及`cropsname`和`varieties`列。 - `from zhnl_area_crops t`:从`zhnl_area_crops`表中选择数据,并使用别名`t`表示该表。 - `LEFT JOIN zhnl_crops c on t.cropsid = c.id`:使用左连接将`t.cropsid`和`c.id`两列进行关联,即将`zhnl_crops`表中对应的记录与`zhnl_area_crops`表中的记录进行关联。 - `where 1=1 and t.areasid= #areasid# and t.status = '1'`:设置查询条件,其中`1=1`是为了方便后续拼接其他查询条件,`t.areasid= #areasid#`表示匹配指定的种植区域ID,`t.status = '1'`表示只选择状态为1的记录。 总体来说,这个查询语句的目的是根据种植区域ID查询种植信息,并将种植信息与作物表中的相关信息进行联合查询,以获取完整的种植信息记录。在执行时,需要传入一个名为`areasid`的参数来指定具体的种植区域ID。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值