自定义监听器监听数据库中数据的变化
此程序只是一个简单的例子,主要监听数据库中
Location 的数据。
先写一个 Event 继承 EventObject
Java 代码
package com.lbx.listong.servlet; import java.util.EventObject; extends EventObject {
private static final long
serialVersionUID = 1L; private Object obj;
public class LocationEvent
public LocationEvent(Object source)
super(source);
obj = source; public Object getSource(){ obj;
return
然后写一个接口继承 EventListener
Java 代码
package com.lbx.listong.servlet;
import
java.util.EventListpublic ener; interface LocationListener extends EventListener
public void
locationEvent(LocationEvent
le); }
测试这个监听,实现他的接口
Java 代
码
package impor com.lbx.listong.servlet; t java.util.concurrent.Execuimpor
tors; t
java.util.concurrent.ScheduledExecutorSeimpor
rvice; t
java.util.concurrent.Timeimpor
Unit; t
com.lbx.listong.service.LocationSeimpor rvice; t
com.lbx.listong.thread.ListThpublic read; class TestLocationListener implements
LocationListener
private publi
LocationService ls; c
TestLocationListenerls = () { new LocationService(); ls.addLocationListener(this);
//注册监听
System.out.println(\添加监听器完毕 \
// 调用此方法触发事件 ,触发的
事件就 是执行 locationEvent( 接口的方
法 )的方法 ls.addLocation(\
tb_location
(companyId,mobile,longitude,latitude,locationTime)
values(2,'444','4','4','2011-03-23 11:03:04')\catch (Exception
ex)
ex.printStackTrace
(); public void @Override
locationEvent(LocationEvent
le) // TODO Auto-generated method { stub
ScheduledExecutorService executor =
Executors.newSingleThreadScheduledExecutor
();
executor.scheduleWithFixedDelay(new ListThread(), 0, 2, TimeUnit.SECONDS);
try {
LocationService 的代码,主要是一些业务的代
码,里面只
写了增加一个记录触发一个事件
Java 代
码
import
package
com.lbx.listong.service;