1。前段时间开发了个WAP上聊天室的机器人。流程图如下
2.数据模型
3.Main Source:
RobotBeat.java
package pubchat;
import java.io.*;
import java.net.*;
import java.util.*;
import pubchat.dao.*;
import pubchat.client.*;
import org.apache.log4j.Logger;
import pubchat.wap.ClientTest;
public class RobotBeat extends Thread {
Logger log = Logger.getLogger(RobotBeat.class);
int timediff; //组件重画的间隔时间
volatile boolean shouldrun; //设为false为停止线程
ChatDAO chatDao = null;
RobotDAO robotDao = null;
Vector roomVec = null;
int roomSize = 0;
Vector robotVec = null;
boolean clearFlg = false;
private static int wordsCount = 0;
public RobotBeat() {
this.timediff = 1 * 60 * 1000;
this.shouldrun = true;
System.out.println("机器人线程启动。。。。。");
this.chatDao = new ChatDAO(true);
this.robotDao = new RobotDAO(true);
this.roomVec = chatDao.selectChatRoom(); //大类,小类,房间id, 房间名,房间类
this.roomSize = this.roomVec.size();
this.robotInit();
}
public void run() {
while (shouldrun) {
try {
//robotInit();
//robotClear();
//robotInit();
wordsCount++;
long start = System.currentTimeMillis();
System.out.println("Sleep start:"+start);
sleep(timediff);
long end = System.currentTimeMillis();
System.out.println("Sleep End:"+end);
//随机说话
// if (this.getProperty()==Driver.FLT_ROBOT) {
//机器人线程
if (wordsCount%20==0) {
System.gc();
robotClear();
robotInit();
} else {
ClientTest content = new ClientTest();
for (int i = 0; i < robotVec.size(); i++) {
String sayId = (String) robotVec.elementAt(i);
String sayWords = this.robotDao.selectRandomWord();
System.out.println("机器人 " + sayId + ":" + sayWords);
content.say(sayId, sayWords);
}
content = null;
}
//robotClear();
//robotInit();
//robotInit();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* Clear the robot user
*/
private void robotClear() {
//Clear robot from userHash
//Vector beats = Driver.query_heart_beats();
//System.out.println("Driver.query_heart_beats:"+beats.size());
for (int i = 0; i <this.robotVec.size(); i++) {
Client c = (Client) Driver.userHash.get(robotVec.elementAt(i));
if (c != null) {
System.out.println("c.Property:"+c.getProperty());
if (c.getProperty() == Driver.FLT_ROBOT) {
System.out.println("移除机器人 ID:"+c.getId());
Driver.removeUser(c.getId());
c.move_player("");
}
}
}
this.robotVec = null;
}
/**
* Init the robot user
*/
private void robotInit() {
this.robotVec = new Vector();
Vector robotVec = robotDao.selectRandomList(roomSize);
System.out.println("RoomSize:"+roomSize+"============"+roomVec.size());
for (int i = 0; i < roomSize; i++) {
//login the robot
String[] strRoom = (String[]) roomVec.elementAt(i);
System.out.println("Roomid:--->"+strRoom[2]);
String[] strRobot = (String[]) robotVec.elementAt(i);
//Roomid strs[2]
//Robot login
robotLogin(strRobot, strRoom[2]);
}
}
/**
* Login the robot user and move to the specify room
* @param argUserid String[]
* @param argRoomid String
* @return int
*/
private int robotLogin(String[] argUserid, String argRoomid) {
System.out.println("robotLogin:User->" + argUserid[1]+argUserid[0] + "到房间->" +
argRoomid);
Client client = (Client) Driver.userHash.get(argUserid);
if (client != null) {
log.info(argUserid + "a已经登录过了a");
return 0;
} else {
client = new RobotClient(argUserid[0]);
client.setShortname(argUserid[1]);
client.create();
Driver.add_User(client);
client.move_player(argRoomid);
this.robotVec.add(argUserid[0]);
return 1;
}
}
}
RobotDao.java
package pubchat.dao;
import java.util.Vector;
public class RobotDAO extends AbstractDAO {
public RobotDAO(boolean isPool) {
super(isPool);
}
/* The default answer of question */
static final String DEFAULT_ANSWER = "我们换个话题吧:)";
/* SQL - Get the robot list */
static final String SELECT_RANDOM_LIST = "Select id,nickname,status,last_change,reg_time,birthday,sex,flag "+
" From (Select id,nickname,status,last_change,reg_time,birthday,Decode(sex,'0','女','男') sex,flag "+
" From t_robot_list "+
" Order by dbms_random.random) "+
" Where rownum<=? ";
/* SQL - Get the random word */
static final String SELECT_RANDOM_WORD = "Select content "+
" From (Select content "+
" From t_robot_default "+
" Order by dbms_random.random) "+
" Where rownum<=1 ";
/* SQL - Get the answer by question */
static final String SELECT_A_BY_Q = "Select ANSWER "+
" From (Select ANSWER "+
" From t_robot_answer a, t_robot_question q, t_robot_exc e "+
" Where a.msg_id = e.a_id "+
" And e.q_id = q.msg_id "+
" And q.content like ? "+
" Order by dbms_random.random) "+
" Where rownum <= 1 ";
/* SQL - Add the new question to the table */
static final String ADD_QUESTION = "Insert into t_robot_question "+
" (msg_id, content) " +
"Values "+
" (seq_t_robot_question_id.nextval, ?) ";
/**
* Get the random list of robot
* @param argCount int
* @return Vector
*/
public Vector selectRandomList(int argCount) {
String[] arrPara = {String.valueOf(argCount)};
return this.selectVecData(SELECT_RANDOM_LIST,arrPara,8);
}
/**
* Get the random word
* @return String
*/
public String selectRandomWord() {
String retStr = "";
Vector v = this.selectVecData(SELECT_RANDOM_WORD,new String[]{},1);
if (v.size()>0) {
String[] arrTemp = (String[])v.get(0);
retStr = arrTemp[0];
}
return retStr;
}
/**
* Get the answer by question
* @param argContent String
* @return String
*/
public String selectQuestionByAnswer(String argContent) {
String retStr = DEFAULT_ANSWER;
String[] arrPara = {String.valueOf(argContent)};
Vector v = this.selectVecData(SELECT_A_BY_Q,arrPara,1);
if (v.size()>0) {
String[] arrTemp = (String[]) v.get(0);
retStr = arrTemp[0];
}
// } else {
// this.addQuestion(argContent);
// }
return retStr;
}
public void addQuestion(String argContent) {
String[] arrPara = {String.valueOf(argContent)};
this.updateData(ADD_QUESTION,arrPara);
}
}