ecmserver2.java
来自「《移动Agent技术》一书的所有章节源代码。」· Java 代码 · 共 531 行 · 第 1/2 页
JAVA
531 行
String sgoodsname;
String smodel;
//float sprice;
//int samount;
String sprice;
String samount;
String[][] services;
int sorder; ///记录当前服务位置
try{
srs=tt.recordSql("select * from service");
services=tt.recordPrint(srs);
srs.close();
int l=services.length;
sorder=1;
srs=tt.recordSql("select * from service");
outLoop:
for (sorder=1;sorder<l;sorder++){
once=services[sorder][8];
System.out.println("once="+once);
srs.next();
System.out.println("go srs ");
try{ cnum=srs.getLong("num");
}
catch(Exception e){ System.out.println("srs.getLong() error");
}
System.out.println("get num: "+cnum);
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";
//////////////
System.out.println("query result got");
while (rs.next()){
if (once=="yes"){
System.out.println("************1");
goods[no][numCols-1]="source";
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;
//**** change the once tag in the service table
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
scon=DriverManager.getConnection("jdbc:odbc:"+"Ecmdb");
}
catch(ClassNotFoundException e){
System.out.println("ClassNotFoundExeption");
}
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("");
}
}
PreparedStatement spstm;
spstm=scon.prepareStatement("UPDATE service SET once='no' WHERE num=?");
spstm.setLong(1,cnum);
spstm.executeUpdate();
spstm.close();
scon.close();
//**** end ****
}//end if
else{
System.out.println("************11");
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<=(0.2*60*1000*aglet.cycletime)){ ///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 else
}//end while
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();
srs.close();
System.out.println("-------i will sleep "+60*1000*(long)aglet.cycletime*0.2+" milli second--------");
long sleeptime=12*1000*(long)aglet.cycletime;
//sleep(60*1000*(long)aglet.cycletime);
//sleep(sleeptime);
sleep(sleeptime);
}///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 + -
显示快捷键?