⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ecmserver.java

📁 IBM AGLET及JAVA实现的电子商务系统源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package ecmAglet;

import com.ibm.aglet.*;
import com.ibm.aglet.event.*;
import java.io.*;
import java.net.URL;
import java.sql.*;
//import java.util.Date;
import java.util.Enumeration;

//import java.util.Date;

public class ecmServer extends Aglet implements Runnable{
	tableSql t;
	log log;
	AgletProxy slaveProxy=null;
	AgletID slaveid=null;
	//AgletProxy ecmClient;
	AgletID self_id=null;
	String slavename;
	Thread repeatServer;
	//long repeattime=0;
	boolean isrun=true;
	boolean isrepeate=true;
	String surl=null;
	double currenttime=0;
	double cycletime=0;
	double servicetime=0;
	//String once;
	
	public void onCreation(Object o) {
		t=new tableSql();
		log = new log();
		log.Init("Ecmdb");
		
		self_id=getAgletID();
		surl = getAgletContext().getHostingURL().toString();
		//repeatServer=new repeatThread(this);
		//repeatServer.start();

		addMobilityListener(new MobilityAdapter() {
	    	public void onArrival(MobilityEvent ev) {
				
				///////////
				//AgletInfo inf=getAgletInfo();
				
				AgletContext cxt=getAgletContext();
	       try{
				Enumeration aglets=cxt.getAgletProxies();
				while (aglets.hasMoreElements()) {			
					AgletProxy tmp=(AgletProxy) aglets.nextElement();
					
					AgletInfo inf=tmp.getAgletInfo();
					System.out.println(inf.toString() );
				   String agName=tmp.getAgletClassName();
					if (agName.endsWith("ecmProxy") ){ ////get server id
						//slaveid=tmp.getAgletID();
						slavename=tmp.getAgletClassName();
					}
				}
				////////////
				//System.out.println(inf.toString() );
				//System.out.println(cxt.toString() );
				//try {
				} catch (Exception ex) {
		   		 ex.printStackTrace();
				}
	      }
                 catch(Exception e)
                 {
                  e.printStackTrace();
                  }
                  }//end onarrival()
  	
		});//end listerner
	}///end function
		
	public boolean handleMessage(Message msg) {
		System.out.println("*******enter deal with message******");
	   
	   if (msg.sameKind("connect")) {
	   	//msgg[order++]=msg;
	   	String[] mm=(String[])msg.getArg();
	   	String proxy=(String)mm[0];
	   	String curl=(String)mm[1];
	   	String cid=(String)mm[2];
	   	String mode=(String)mm[3];  ////////mode (repeate or once)
	   	String str=(String)mm[4];    ////command
	   	log.Add("event","ecmserver:recieve command from "+curl+"//id is:"+cid);
	   	
	   	slaveid=new AgletID(mm[0]);
	   	try{
	   		slaveProxy=getAgletContext().getAgletProxy(slaveid);
	    		
			}   ////
			catch (Exception e){
				e.printStackTrace();
				System.out.println("connect with server failed");
				log.Add("error","ecmserver:connect with server failed");

			}  
			
			/////////获取系统当前服务时间,及服务周期
				ResultSet rs;
				try{
	   			t.tableInit("Ecmdb");  
	   			rs=t.recordSql("select * from ecmSys"); 
   				while (rs.next()){				
						//repeattime=Long.parseLong(rs.getString("repeattime") );
						//System.out.println("the cycle time is:"+rs.getString("cycle") );
						//System.out.println("the servicetime of day is:"+rs.getString("servicetime") ); ///day
						
						cycletime=Double.valueOf(rs.getString("cycle")).doubleValue();
						//System.out.println("***the cycle time of minutes is:"+cycletime);
						servicetime=Double.valueOf(rs.getString("servicetime")).doubleValue();
						//currenttime=(double)System.currentTimeMillis()-cycletime;
						//System.out.println("***the service currenttime is:"+currenttime);
				}	
				t.closeResult();
				t.closeConnect();
                                    }//end while
				}//////end try
				catch(NumberFormatException ex){
					ex.printStackTrace();
					log.Add("error","ecmserver: set repeattime FormatException");
				}
				catch(SQLException ex){
					log.Add("error","ecmserver:get repeattime Exception ");
					System.out.println("\n***SQLException caught ***\n");
					while (ex != null){
						System.out.println("SQLState:"+ex.getSQLState());
						System.out.println("Message:"+ex.getMessage());	
						System.out.println("Vendor:"+ex.getErrorCode());
						ex=ex.getNextException();
						System.out.println("");
					}
				}			
			////////
			//currenttime=(double)System.currentTimeMillis()-cycletime*1000*60; //////设定第一次系统启动服务时间
			currenttime=(double)System.currentTimeMillis(); //////设定第一次系统启动服务时间
			System.out.println("***the current system time is:"+System.currentTimeMillis());
			System.out.println("***the first service time is:"+currenttime);
			///////////
			
			if (isrun){
	   		repeatServer=new repeatThread(this);
	   		repeatServer.start();
	   		isrun=false;
	   	}///end if
	   	
			/////记录用户查询要求
			//once=true;
			if (mode.equals("repeate")){
				System.out.println("the mode is:"+mode);
				System.out.println("the command is:"+str);
				double s=(double)System.currentTimeMillis()+servicetime*24*60*60*1000;
				String ss=new Double(s).toString();
				
				System.out.println("***the cycle time of minutes is:"+cycletime);
				//System.out.println("***the service currenttime is:"+currenttime);
				System.out.println("***this support service time of repeate is:"+ss);
				
				insertEcm(str,curl,cid,ss);
				t.closeResult();
			}
			else {
				double s=(double)System.currentTimeMillis()+30*1000;
				//double s=currenttime+100;
				String ss=new Double(s).toString();
				
				System.out.println("***the cycle time of minutes is:"+cycletime);
				System.out.println("***the service currenttime is:"+currenttime);
				System.out.println("***this support service time of once is:"+ss);
				
				insertEcm(str,curl,cid,ss);
				t.closeResult();
				
			}
	   	//System.out.println("ecmServer have succeed to save the guest command ");
	   	
	   	return true;
			
	   }
	   else if (msg.sameKind("bye")) {
	   	System.out.println("bye and close the server connection");
	   	slaveid=null;
	   	return true;
	   }
		return false;
	}
   	
	public void insertEcm(String strQuery,String curl,String cid,String servicetime) {
   	String tmpstr;
   	int recordNumber;
   	String[] records=null;
   	ResultSet rs;
		try{
			///query db and display result
			t.tableInit("Ecmdb");      ////////strQuery :'good1','100','100','5000'
			String once="yes";
			
			strQuery="insert into service (curl,cid,goodsname,model,price,amount,servetime,once) values("
			          +"'"+curl+"','"+cid+"',"+strQuery+",'"+servicetime+"','"+once+"')"; 
			
			t.recordInsert(strQuery);
			System.out.println(strQuery);
			rs=t.recordSql("select * from service");
			t.display(t.recordPrint(rs) );
			slaveProxy.sendAsyncMessage(new Message("bye","kill finderProxy") );
			log.Add("event","ecmserver:kill ecmproxy");
			
			t.closeConnect();
		}///end try
		catch (Exception e){
			e.printStackTrace();
			log.Add("error","ecmserver:insert new service failed");
		}  
		   			
	}//end function
			   	
      	
}	   	
////////// 
class repeatThread extends Thread{
	ecmServer aglet=null;
	//long repeattime=0;
	double repeattime=0;
	double preRepeattime=0;
	boolean isfirst=true;
	long cnum; 
	String once;
	Connection scon;
	//boolean once=true;
	//Message[] msgg;
	
	//AgletProxy slaveProxy=null;
	//AgletID slaveid=null;
	AgletProxy ecmClient;
	
	repeatThread(ecmServer aglet){
		this.aglet = aglet;
	}
	
	public void run(){
		System.out.println("*******enter the thread run********"); 
		aglet.log.Add("event","ecmserver:enter recicle service ");
		//filter=false;
		tableSql tt=new tableSql();
		ResultSet rs; //for result of goods database table
		ResultSet srs; //for result of service database table
		int datetime;
		int firsttime;
		int times=0;
		tt.tableInit("Ecmdb");
		
	while (true){
		/////获取最近一次轮询时间repeattime
		if (isfirst){
			repeattime=aglet.currenttime;
			preRepeattime=aglet.currenttime;
			isfirst=false;	
			
		}///end if
		System.out.println("");
		System.out.println("-------this time is:"+(times++)+"---------");
		System.out.println("now repeat time of milli seconds is :"+repeattime);
		System.out.println("current system time of milli seconds is :"+System.currentTimeMillis());
		System.out.println("-------of second:"+(System.currentTimeMillis()-repeattime)/1000 );
		//////根据客户信息service表查询新的商品信息,有则返回给客户
		//long goodstime=0;
		double goodstime=0;
		String[][] goods;
		int numCols=0,no=0;
		ResultSetMetaData rsmd;
		String tmpstr;
		String sgoodsname;
		String smodel;
		//float sprice;
		//int samount;
		String sprice;
		String samount;
		String[][] services;
		int sorder;     ///记录当前服务位置

		try{	
			srs=tt.recordSql("select * from service");
			ResultSetMetaData srsmd=srs.getMetaData();
   			int snumCols=srsmd.getColumnCount();
   			System.out.println("columncount:"+snumCols);
			services=tt.recordPrint(srs);
			srs.close();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -