自定義監聽器監聽資料庫中資料的變化
阿新 • • 發佈:2019-02-11
此程式只是一個簡單的例子,主要監聽資料庫中Location的資料。
先寫一個Event繼承EventObject
Java程式碼- package com.lbx.listong.servlet;
- import java.util.EventObject;
- publicclass LocationEvent extends EventObject {
- privatestaticfinallong 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;
- publicinterface LocationListener extends EventListener {
-
public
- }
- 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
- publicclass 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
- publicvoid 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;
- publicclass 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那麼要注意同步問題
- publicvoid addLocationListener(LocationListener ll)
- {
- repository.addElement(ll);//這步要注意同步問題
- }
- //如果這裡沒有使用Vector而是使用ArrayList那麼要注意同步問題
- publicvoid notifyLocationEvent(LocationEvent event) {
- Enumeration e = repository.elements();//這步要注意同步問題
- while(e.hasMoreElements())
- {
- ll = (LocationListener)e.nextElement();
- ll.locationEvent(event);
- }
- }
- //刪除監聽器,如果這裡沒有使用Vector而是使用ArrayList那麼要注意同步問題
- publicvoid 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;
- }
- publicvoid 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));
- }
- }
- publicstaticvoid main(String[] args){
- new TestLocationListener();
- }
- }