ecmserver1.java

来自「《移动Agent技术》一书的所有章节源代码。」· Java 代码 · 共 476 行 · 第 1/2 页

JAVA
476
字号
		int datetime;
		int firsttime;
		int times=0;
		tt.tableInit("Ecmdb");
		
	while (true){
		/////获取最近一次轮询时间repeattime
		if (isfirst){
			repeattime=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{
			rs=tt.recordSql("select * from service");
			services=tt.recordPrint(rs);
			rs.close();
			
			int l=services.length;
			sorder=1;
			outLoop:
			for (sorder=1;sorder<l;sorder++){
				no=0;
			  ////////
			  System.out.println("");
			  System.out.println("-------enter a new service-------");
			  System.out.println(System.currentTimeMillis());
			  			  			  
			  if (services[sorder][1]==null ){
					tt.closeResult();
					break outLoop; 
				}
			  ////////
			  else { 
				//long servetime=Long.parseLong(services[sorder][7]);cycletime=Double.valueOf(rs.getString("cycle") ).doubleValue();
				double servetime=Double.valueOf(services[sorder][7] ).doubleValue();
				System.out.println("service time is :"+servetime);
				///////超过给定服务时间则删除该服务
				if (servetime<repeattime){
					aglet.log.Add("event","ecmserver:servetime("+servetime+") outdate and will del "); 
					tt.recordDelete("delete from service where service.servetime='"+servetime+"'");
					System.out.println("delete from service where service.servetime='"+servetime+"'");
				}
				///////在规定服务时间内则执行该服务
				else {
					
					sgoodsname=services[sorder][3];
					smodel=services[sorder][4];
					sprice=services[sorder][5];
					samount=services[sorder][6];
					
					String condition=" where goodsname='"+sgoodsname+"'";
					
					if (!smodel.equals(""))
						condition=condition+" and model='"+smodel+"'";
					if (!sprice.equals("0"))
						condition=condition+" and model"+sprice;
					if (!samount.equals("0"))
						condition=condition+" and amount"+samount;
					System.out.println("the condition is:"+condition);
				
					//////////查询是否有新信息
					rs=tt.recordSql("select * from goods "+condition);
					System.out.println("select * from goods  "+condition);
					
					rsmd=rs.getMetaData();
   				numCols=rsmd.getColumnCount();
   				goods=new String[100][numCols-1+1];
   				//display each column title
   				for (int i=1;i<=numCols-1;i++){
   					goods[no][i-1]=rsmd.getColumnLabel(i);
   				}
   				goods[no][numCols-1]="source";
   				//////////////
   				if (aglet.once){
   					System.out.println("************1");
   					
						
   						while (rs.next()){
   							String gtime=rs.getString("goodstime");				
								goodstime=Double.valueOf(gtime).doubleValue();
								//System.out.println("goods time is :"+goodstime);
								long ggtime=(long)goodstime;
								java.util.Date  gdate=new java.util.Date(ggtime);
								String sdate=gdate.toString();
								System.out.println("goods time is :"+sdate);
								
								no++;
								for(int i=1;i<numCols-1;i++){
									//if (i>1) System.out.print(",");
									tmpstr=rs.getString(i);			
									if(rs.wasNull()){
										goods[no][i-1]="NULL";
										//System.out.print(goods[no][i-1]);
									}
									else {
										goods[no][i-1]=tmpstr;
										//System.out.print("****"+goods[no][i-1]);
									}
								}///end for
								goods[no][numCols-2]=gtime;
								//goods[no][numCols-2]=sdate;
								goods[no][numCols-1]=aglet.surl;
							
							}///end while	
							aglet.once=false;
					}
					else{
					System.out.println("************11");
   				while (rs.next()){
   					String gtime=rs.getString("goodstime");				
						//goodstime=Long.parseLong(gtime);
						goodstime=Double.valueOf(gtime).doubleValue();
						//System.out.println("goods time is :"+goodstime);
						long ggtime=(long)goodstime;
						java.util.Date  gdate=new java.util.Date(ggtime);
						String sdate=gdate.toString();
						System.out.println("goods time is :"+sdate);
						
						if ( repeattime-goodstime<=60*1000*aglet.cycletime*0.2){        ///cycletiem单位为分钟 
							
							no++;
							//System.out.println("the no is:"+no);
   						//display each column data, "numCols-1 " means not include "descirbe"
							for(int i=1;i<numCols-1;i++){
								//if (i>1) System.out.print(",");
								tmpstr=rs.getString(i);			
								if(rs.wasNull()){
									goods[no][i-1]="NULL";
									//System.out.print(goods[no][i-1]);
								}
								else {
									goods[no][i-1]=tmpstr;
									//System.out.print("****"+goods[no][i-1]);
								}
							}///end for
							goods[no][numCols-2]=gtime;
							//goods[no][numCols-2]=sdate;
							goods[no][numCols-1]=aglet.surl;
							
						}///end if
					}///end while	
					}///end else
			   	rs.close();
	  				//////////将查询到的新信息返回给客户
					int l2=goods.length;
					int ll=goods[1].length;
					//System.out.println("array lenth is :"+l2);
					//System.out.println("record lenth is :"+ll);
					if (l2<2)
						System.out.println("sorry,there is no a new goods");
					else {
						URL urlClient=null;
						AgletID idClient;
	   				System.out.println("-------enter send result--------"); 
	    				//System.out.println("the sorder is:"+sorder);
	    				//System.out.println("ecmClient url are: "+services[sorder][1]);
	    				try{
	    					urlClient=new URL(services[sorder][1]);
	    					System.out.println("ecmClient id are: "+services[sorder][2]);
						}
						catch(java.net.MalformedURLException ex){
							ex.printStackTrace();
						}
						try{
							idClient=new AgletID(services[sorder][2]);
	       	 			ecmClient = aglet.getAgletContext().getAgletProxy(urlClient, idClient);
	    					ecmClient.sendAsyncMessage(new Message("getResult",goods));
	    				}
	    				catch(InvalidAgletException ex){
	    					aglet.log.Add("error","ecmserver:InvalidAglet("+services[sorder][2]+"),fail to send reseult and will del it"); 
							tt.recordDelete("delete from service where service.cid='"+services[sorder][2]+"'");
							System.out.println("delete from service where service.cid='"+services[sorder][2]+"'");
							ex.printStackTrace();
						}
						tt.closeResult();
	  				}
				}///end else2
			 }///end else3                   
			}///end for	
			//tt.closeConnect();
			
			System.out.println("-------i will sleep "+60*1000*(long)aglet.cycletime*0.2+" milli second--------");	
			long sleeptime=60*1000*(long)aglet.cycletime;
			//sleep(60*1000*(long)aglet.cycletime);	
			//sleep(sleeptime);
			sleep(12*1000*aglet.cycletime);
		}///end try
		catch(SQLException ex){
			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("");
			}
			aglet.log.Add("error","ecmserver:query date in this service");
		}
		catch(NumberFormatException ex){
				ex.printStackTrace();
				aglet.log.Add("error","ecmserver:FormatException ");
		}
		catch(InterruptedException ex){
				ex.printStackTrace();
				aglet.log.Add("error","ecmserver:Interrupted");
		}
		
		System.out.println("New");
		System.out.print("time to old: ");
		System.out.println(goodstime-repeattime);
		//repeattime=System.currentTimeMillis()-3*60*1000; 
		repeattime=System.currentTimeMillis(); 	
		
	}///end while
	}///end function
	//////////	
}	   	

⌨️ 快捷键说明

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