此程序只是一个简单的例子,主要监听数据库中Location的数据。
先写一个Event继承EventObject package com.lbx.listong.servlet;
import java.util.EventObject;
public class LocationEvent extends EventObject {
private static final long serialVersionUID = 1L;
private Object obj;
public LocationEvent(Object source) {
super(source);
obj = source;
}
public Object getSource(){
return obj;
}
}
然后写一个接口继承EventListenerpackage com.lbx.listong.servlet;
import java.util.EventListener;
public interface LocationListener extends EventListener {
public void locationEvent(LocationEvent le);
}
测试这个监听,实现他的接口package com.lbx.listong.servlet;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import com.lbx.listong.service.LocationService;
import com.lbx.listong.thread.ListThread;
public class TestLocationListener implements LocationListener {
private LocationService ls;
public TestLocationListener() {
ls = new LocationService();
ls.addLocationListener(this); //注册监听
System.out.println("添加监听器完毕");
try {
// 调用此方法触发事件,触发的事件就是执行locationEvent(接口的方法)的方法
ls.addLocation("insert into tb_location (companyId,mobile,longitude,latitude,locationTime) values(2,'444','4','4','2011-03-23 11:03:04')");
} catch (Exception ex) {
ex.printStackTrace();
}
}
@Override
public void locationEvent(LocationEvent le) {
// TODO Auto-generated method stub
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
executor.scheduleWithFixedDelay(new ListThread(), 0, 2, TimeUnit.SECONDS);
}
}
LocationService的代码,主要是一些业务的代码,里面只写了增加一个记录触发一个事件 package com.lbx.listong.service;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Vector;
import com.lbx.listong.DBUtil.DB;
import com.lbx.listong.servlet.LocationEvent;
import com.lbx.listong.servlet.LocationListener;
import com.lbx.listong.servlet.TestLocationListener;
public class LocationService {
private Connection conn = null;
List list = new ArrayList();
String sql = "select * from tb_location";
Statement st = null;
private Vector repository = new Vector();
private LocationListener ll;
public LocationService()
{
}
//注册监听器,如果这里没有使用Vector而是使用ArrayList那么要注意同步问题
public void addLocationListener(LocationListener ll)
{
repository.addElement(ll);//这步要注意同步问题
}
//如果这里没有使用Vector而是使用ArrayList那么要注意同步问题
public void notifyLocationEvent(LocationEvent event) {
Enumeration e = repository.elements();//这步要注意同步问题
while(e.hasMoreElements())
{
ll = (LocationListener)e.nextElement();
ll.locationEvent(event);
}
}
//删除监听器,如果这里没有使用Vector而是使用ArrayList那么要注意同步问题
public void removeLocationListener(LocationListener ll)
{
repository.remove(ll);//这步要注意同步问题
}
public List getLocationList(){
conn = DB.getConnection();
try {
st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
while(rs.next()){
String longitude = rs.getString("longitude");
list.add(longitude);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
public void addLocation(String sql){
boolean b = false;
conn = DB.getConnection();
try {
st = conn.createStatement();
int id = st.executeUpdate(sql);
if(id>0){
b = true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//添加一个记录成功就通知事件,然后执行接口的方法
if(b){
System.out.println("sss");
notifyLocationEvent(new LocationEvent(this));
}
}
public static void main(String[] args){
new TestLocationListener();
}
}
package com.lbx.listong.servlet;
import java.util.EventObject;
public class LocationEvent extends EventObject {
private static final long serialVersionUID = 1L;
private Object obj;
public LocationEvent(Object source) {
super(source);
obj = source;
}
public Object getSource(){
return obj;
}
}
package com.lbx.listong.servlet;
import java.util.EventListener;
public interface LocationListener extends EventListener {
public void locationEvent(LocationEvent le);
}
package com.lbx.listong.servlet;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import com.lbx.listong.service.LocationService;
import com.lbx.listong.thread.ListThread;
public class TestLocationListener implements LocationListener {
private LocationService ls;
public TestLocationListener() {
ls = new LocationService();
ls.addLocationListener(this); //注册监听
System.out.println("添加监听器完毕");
try {
// 调用此方法触发事件,触发的事件就是执行locationEvent(接口的方法)的方法
ls.addLocation("insert into tb_location (companyId,mobile,longitude,latitude,locationTime) values(2,'444','4','4','2011-03-23 11:03:04')");
} catch (Exception ex) {
ex.printStackTrace();
}
}
@Override
public void locationEvent(LocationEvent le) {
// TODO Auto-generated method stub
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
executor.scheduleWithFixedDelay(new ListThread(), 0, 2, TimeUnit.SECONDS);
}
}
package com.lbx.listong.service;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Vector;
import com.lbx.listong.DBUtil.DB;
import com.lbx.listong.servlet.LocationEvent;
import com.lbx.listong.servlet.LocationListener;
import com.lbx.listong.servlet.TestLocationListener;
public class LocationService {
private Connection conn = null;
List list = new ArrayList();
String sql = "select * from tb_location";
Statement st = null;
private Vector repository = new Vector();
private LocationListener ll;
public LocationService()
{
}
//注册监听器,如果这里没有使用Vector而是使用ArrayList那么要注意同步问题
public void addLocationListener(LocationListener ll)
{
repository.addElement(ll);//这步要注意同步问题
}
//如果这里没有使用Vector而是使用ArrayList那么要注意同步问题
public void notifyLocationEvent(LocationEvent event) {
Enumeration e = repository.elements();//这步要注意同步问题
while(e.hasMoreElements())
{
ll = (LocationListener)e.nextElement();
ll.locationEvent(event);
}
}
//删除监听器,如果这里没有使用Vector而是使用ArrayList那么要注意同步问题
public void removeLocationListener(LocationListener ll)
{
repository.remove(ll);//这步要注意同步问题
}
public List getLocationList(){
conn = DB.getConnection();
try {
st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
while(rs.next()){
String longitude = rs.getString("longitude");
list.add(longitude);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
public void addLocation(String sql){
boolean b = false;
conn = DB.getConnection();
try {
st = conn.createStatement();
int id = st.executeUpdate(sql);
if(id>0){
b = true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//添加一个记录成功就通知事件,然后执行接口的方法
if(b){
System.out.println("sss");
notifyLocationEvent(new LocationEvent(this));
}
}
public static void main(String[] args){
new TestLocationListener();
}
}