📄 emailclient.java
字号:
/**
* Created at Dec 4, 2008
*/
package com.jdev.app.db;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.Properties;
import com.jdev.db.ConnectPool;
import com.jdev.net.config.ServerCfg;
import com.jdev.net.connector.Connection;
import com.jdev.net.connector.ConnectionFactory;
import com.jdev.net.event.EventAdapter;
import com.jdev.net.event.Notifier;
import com.jdev.net.queue.Request;
import com.jdev.net.queue.Response;
import com.jdev.util.Debug;
/**
* <p>Title: EmailClient</p>
* <p>Description: </p>
* @author Lawrence
* @version
*/
public class EmailClient extends EventAdapter implements Runnable {
private EmailQueueHelper emailDataHelper;
public final static int DEF = 0;
public final static int SMS = 1;
public final static int EMAIL = 2;
public final static int FIDS = 3;
private java.sql.Connection conn;
private ConnectPool connectionManager;
private String email;
private ConnectionFactory connFactory = ConnectionFactory
.getInstance(ConnectionFactory.CLIENT);
private final static String module = DbClient.class.getName();
protected Notifier notifier;
private Connection client;
private String appType = "email";
// private EmailPush emailPush;
private boolean bStartEmailPush = false;
private static int MAX_THREADS = 10;
/**
*
*/
public EmailClient() {
// 获取事件触发器
notifier = Notifier.getNotifier();
connectionManager = ConnectPool.getInstance();
emailDataHelper = new EmailQueueHelper(ConnectionFactory.CLIENT);
// emailPush = new EmailPush();
}
/* (non-Javadoc)
* @see com.jdev.net.event.EventAdapter#onRead(com.jdev.net.queue.Request)
*/
@Override
public void onRead(Request request) throws Exception {
// super.onRead(request);
String recv = new String(request.getDataInput());
if(recv.equalsIgnoreCase("ok")) {
// 回复收到信息,略过
return;
}
if (appType.equalsIgnoreCase("email")) {
if(!bStartEmailPush) {
Thread s = new Thread(new EmailPush("smtp.sohu.com","\"合智创展\"<gwt-tech@sohu.com>","gwt-tech","gwtgwt"));
s.start();
// Thread s1 = new Thread(new EmailPush("","","",""));
// s1.start();
}
emailDataHelper.writeSend(recv);
bStartEmailPush = true;
}
}
/* (non-Javadoc)
* @see com.jdev.net.event.EventAdapter#onWrite(com.jdev.net.queue.Request, com.jdev.net.queue.Response)
*/
@Override
public void onWrite(Request request, Response response) throws Exception {
if (request == null)
return;
response.setDataInput(emailDataHelper.readRecv().getBytes());
// super.onWrite(request, response);
}
public void openDb(String DriverName, String connString, String userName,
String password) throws Exception {
// // 1. 注册驱动
// try {
// Class.forName(DriverName);
// } catch (ClassNotFoundException e) {
// e.printStackTrace();
// }// Mysql 的驱动
//
// try {
// // 2. 获取数据库的连接
// conn = DriverManager.getConnection(
// connString,userName,password);
//
// } catch (Exception e) {
// Debug.logError(e, module);
// throw new Exception(e);
// }
conn = connectionManager.getConnection("test");
InputStream is = getClass().getClassLoader().getResourceAsStream(
"db.properties");
Properties dbProps = new Properties();
try {
dbProps.load(is);
} catch (Exception e) {
Debug.logError("不能读取属性文件. " + "请确保db.properties在CLASSPATH指定的路径中");
}
email = dbProps
.getProperty(
"email",
"SELECT * FROM MV_EMAIL_Receiver where receivestate = '0' and id < (select min(id) from MV_EMAIL_Receiver where receivestate = '0')+10");
}
public void close() {
try {
} catch (Exception e) {
Debug.logError(e, module);
}
}
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {
try {
client = connFactory.getConnection();
ServerCfg cfg = new ServerCfg();
// appType = cfg.getAppType();
client.open(cfg.getUrl(), cfg.getTcpPort());
emailDataHelper.writeRecv(appType);
} catch (Exception e) {
notifier.fireOnError("-->Error occured in Connect to Server: "
+ e.getMessage());
}
try {
this.openDb("oracle.jdbc.driver.OracleDriver",
"jdbc:oracle:thin:@192.168.168.121:1521:GWTWOC", "gwtexpo",
"gwtexpo");
} catch (Exception e) {
notifier.fireOnError("-->Error occured in DbClient openDb: "
+ e.getMessage());
}
// 监听
while (!Thread.interrupted()) {
try {
Thread.sleep(1000);
if (conn == null || conn.isClosed()) {
Thread.sleep(10000);
try {
this
.openDb(
"oracle.jdbc.driver.OracleDriver",
"jdbc:oracle:thin:@192.168.168.121:1521:GWTWOC",
"gwtexpo", "gwtexpo");
} catch (Exception e) {
notifier.fireOnError("-->Error occured in DbClient openDB: "
+ e.getMessage());
}
continue;
}
} catch (Exception e) {
notifier.fireOnError("-->Error occured in DbClient: "
+ e.getMessage());
continue;
}
}
}
public static void main(String[] args) throws Exception {
try {
EmailClient d = new EmailClient();
Notifier notifier = Notifier.getNotifier();
notifier.addListener(d);
ReadMailBody rd = new ReadMailBody();
Thread rs = new Thread(rd);
rs.start();
ReadMailText rt = new ReadMailText();
Thread rst = new Thread(rd);
rst.start();
Thread s = new Thread(d);
s.start();
} catch (Exception e) {
throw new Exception(e);
}
}
/* (non-Javadoc)
* @see com.jdev.net.event.EventAdapter#onStatus(java.lang.String, int)
*/
@Override
public void onStatus(String key, int status) {
// super.onStatus(key, status);
java.sql.Statement stmt = null;
try {
emailDataHelper.writeRecv(appType);
// 3. 获取表达式
stmt = conn.createStatement();
if (status == 0) {
StringBuilder strUpdate = new StringBuilder(
"update TB_Email_Receiver set RECEIVESTATE='2' where CLIENTINFOID in (");
strUpdate.append(key);
strUpdate.append(")");
// 读取sms
int len = stmt.executeUpdate(strUpdate.toString());
} else {
StringBuilder strUpdate = new StringBuilder(
"update TB_Email_Receiver set RECEIVESTATE=" + status + " where CLIENTINFOID in (");
strUpdate.append(key);
strUpdate.append(")");
// 读取sms
int len = stmt.executeUpdate(strUpdate.toString());
}
// 6. 释放资源
stmt.close();
} catch (Exception e) {
notifier.fireOnError("-->Error occured in DbClient onStatus: "
+ e.getMessage());
try {
stmt.close();
} catch (SQLException e1) {
notifier.fireOnError("-->Error occured in DbClient stmt.close: "
+ e.getMessage());
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -