1. 程式人生 > >自定義監聽器監聽資料庫中資料的變化

自定義監聽器監聽資料庫中資料的變化

此程式只是一個簡單的例子,主要監聽資料庫中Location的資料。

先寫一個Event繼承EventObject

Java程式碼  收藏程式碼
  1. package com.lbx.listong.servlet;  
  2. import java.util.EventObject;  
  3. publicclass LocationEvent extends EventObject {  
  4.     privatestaticfinallong serialVersionUID = 1L;  
  5.     private Object obj;  
  6.     public LocationEvent(Object source) {  
  7.         super(source);  
  8.         obj = source;  
  9.     }  
  10.     public Object getSource(){  
  11.         return obj;  
  12.     }  
  13. }  
 然後寫一個介面繼承EventListener Java程式碼  收藏程式碼
  1. package com.lbx.listong.servlet;  
  2. import java.util.EventListener;  
  3. publicinterface LocationListener extends EventListener {  
  4.     public
    void locationEvent(LocationEvent le);  
  5. }  
 測試這個監聽,實現他的介面 Java程式碼  收藏程式碼
  1. package com.lbx.listong.servlet;  
  2. import java.util.concurrent.Executors;  
  3. import java.util.concurrent.ScheduledExecutorService;  
  4. import java.util.concurrent.TimeUnit;  
  5. import com.lbx.listong.service.LocationService;  
  6. import
     com.lbx.listong.thread.ListThread;  
  7. publicclass TestLocationListener implements LocationListener {  
  8.     private LocationService ls;  
  9.     public TestLocationListener() {  
  10.         ls = new LocationService();  
  11.         ls.addLocationListener(this);                     //註冊監聽
  12.         System.out.println("新增監聽器完畢");  
  13.         try {  
  14.             // 呼叫此方法觸發事件,觸發的事件就是執行locationEvent(介面的方法)的方法
  15.             ls.addLocation("insert into tb_location (companyId,mobile,longitude,latitude,locationTime) values(2,'444','4','4','2011-03-23 11:03:04')");  
  16.         } catch (Exception ex) {  
  17.             ex.printStackTrace();  
  18.         }  
  19.     }  
  20.     @Override
  21.     publicvoid locationEvent(LocationEvent le) {  
  22.         // TODO Auto-generated method stub
  23.         ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();   
  24.         executor.scheduleWithFixedDelay(new ListThread(), 02, TimeUnit.SECONDS);  
  25.     }  
  26. }  
 LocationService的程式碼,主要是一些業務的程式碼,裡面只寫了增加一個記錄觸發一個事件 Java程式碼  收藏程式碼
  1. package com.lbx.listong.service;  
  2. import java.sql.Connection;  
  3. import java.sql.ResultSet;  
  4. import java.sql.SQLException;  
  5. import java.sql.Statement;  
  6. import java.util.ArrayList;  
  7. import java.util.Enumeration;  
  8. import java.util.List;  
  9. import java.util.Vector;  
  10. import com.lbx.listong.DBUtil.DB;  
  11. import com.lbx.listong.servlet.LocationEvent;  
  12. import com.lbx.listong.servlet.LocationListener;  
  13. import com.lbx.listong.servlet.TestLocationListener;  
  14. publicclass LocationService {  
  15.     private Connection conn = null;  
  16.     List list = new ArrayList();  
  17.     String sql = "select * from tb_location";  
  18.     Statement st = null;  
  19.     private Vector repository = new Vector();  
  20.     private LocationListener ll;  
  21.     public LocationService()  
  22.     {  
  23.     }  
  24.     //註冊監聽器,如果這裡沒有使用Vector而是使用ArrayList那麼要注意同步問題
  25.     publicvoid addLocationListener(LocationListener ll)  
  26.     {  
  27.         repository.addElement(ll);//這步要注意同步問題
  28.     }  
  29.     //如果這裡沒有使用Vector而是使用ArrayList那麼要注意同步問題
  30.     publicvoid notifyLocationEvent(LocationEvent event) {  
  31.         Enumeration e = repository.elements();//這步要注意同步問題
  32.         while(e.hasMoreElements())  
  33.         {  
  34.           ll = (LocationListener)e.nextElement();  
  35.           ll.locationEvent(event);  
  36.         }  
  37.     }  
  38.     //刪除監聽器,如果這裡沒有使用Vector而是使用ArrayList那麼要注意同步問題
  39.     publicvoid removeLocationListener(LocationListener ll)  
  40.     {  
  41.         repository.remove(ll);//這步要注意同步問題
  42.     }  
  43.     public List getLocationList(){  
  44.         conn = DB.getConnection();  
  45.         try {  
  46.             st = conn.createStatement();  
  47.             ResultSet rs = st.executeQuery(sql);  
  48.             while(rs.next()){  
  49.                 String longitude = rs.getString("longitude");  
  50.                 list.add(longitude);  
  51.             }  
  52.         } catch (SQLException e) {  
  53.             // TODO Auto-generated catch block
  54.             e.printStackTrace();  
  55.         }   
  56.         return list;  
  57.     }  
  58.     publicvoid addLocation(String sql){  
  59.         boolean b = false;  
  60.         conn = DB.getConnection();  
  61.         try {  
  62.             st = conn.createStatement();  
  63.             int id = st.executeUpdate(sql);  
  64.             if(id>0){  
  65.                 b = true;  
  66.             }  
  67.         } catch (SQLException e) {  
  68.             // TODO Auto-generated catch block
  69.             e.printStackTrace();  
  70.         }  
  71.         //新增一個記錄成功就通知事件,然後執行介面的方法
  72.         if(b){  
  73.             System.out.println("sss");  
  74.             notifyLocationEvent(new LocationEvent(this));  
  75.         }  
  76.     }  
  77.     publicstaticvoid main(String[] args){  
  78.         new TestLocationListener();  
  79.     }  
  80. }