initial commit
This commit is contained in:
304
nms_sync/src/com/nms/test/TestClass.java
Normal file
304
nms_sync/src/com/nms/test/TestClass.java
Normal file
@@ -0,0 +1,304 @@
|
||||
package com.nms.test;
|
||||
|
||||
import java.sql.CallableStatement;
|
||||
import java.sql.Date;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.jfinal.kit.PropKit;
|
||||
import com.jfinal.kit.StrKit;
|
||||
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
|
||||
import com.jfinal.plugin.activerecord.Db;
|
||||
import com.jfinal.plugin.activerecord.ICallback;
|
||||
import com.jfinal.plugin.activerecord.Record;
|
||||
import com.jfinal.plugin.c3p0.C3p0Plugin;
|
||||
import com.jfinal.plugin.druid.DruidPlugin;
|
||||
import com.mysql.jdbc.Connection;
|
||||
import com.mysql.jdbc.PreparedStatement;
|
||||
import com.mysql.jdbc.Statement;
|
||||
import com.nms.main.Conn;
|
||||
|
||||
public class TestClass {
|
||||
private static Connection getConnection(){
|
||||
String driver ="com.mysql.jdbc.Driver";
|
||||
String url="jdbc:mysql://localhost:3306/nms_sync";
|
||||
String username="root";
|
||||
String password="root";
|
||||
Connection conn=null;
|
||||
try{
|
||||
Class.forName(driver);
|
||||
conn = (Connection) DriverManager.getConnection(url, username, password);
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return conn;
|
||||
}
|
||||
|
||||
private static Connection getNmsConnection(){
|
||||
String driver ="com.mysql.jdbc.Driver";
|
||||
String url="jdbc:mysql://10.0.6.247:3306/nms";
|
||||
String username="nms";
|
||||
String password="nms";
|
||||
Connection conn=null;
|
||||
try{
|
||||
Class.forName(driver);
|
||||
conn = (Connection) DriverManager.getConnection(url, username, password);
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return conn;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testA(){
|
||||
Connection conn = getConnection();
|
||||
System.out.println(conn);
|
||||
PreparedStatement pstmt=null;
|
||||
try {
|
||||
pstmt=(PreparedStatement) conn.prepareStatement("insert into sync_db_info (ip,port,database_name) values (?,?,?)");
|
||||
pstmt.setString(1, "10.0.6.247");
|
||||
pstmt.setInt(2, 8080);
|
||||
pstmt.setString(3,"nms");
|
||||
int id = pstmt.executeUpdate();
|
||||
System.out.println(id);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}finally{
|
||||
try {
|
||||
if(pstmt!=null){
|
||||
pstmt.close();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
conn.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void importData(){
|
||||
Connection nmsConn = getNmsConnection();
|
||||
Connection conn = getConnection();
|
||||
PreparedStatement pstmt=null;
|
||||
PreparedStatement pstmt2=null;
|
||||
try {
|
||||
pstmt=(PreparedStatement)nmsConn.prepareStatement("select table_name as tn from information_schema.tables where TABLE_SCHEMA='nms'");
|
||||
ResultSet resultSet = pstmt.executeQuery();
|
||||
pstmt2=(PreparedStatement)conn.prepareStatement("insert into table_sync_info (table_name,event,last_id,last_date,db_id,mode) values (?,?,?,?,?,?)");
|
||||
while(resultSet.next()){
|
||||
String tableName = resultSet.getString("tn");
|
||||
|
||||
pstmt2.setString(1, tableName);
|
||||
pstmt2.setInt(2, 1);
|
||||
pstmt2.setInt(3, 1);
|
||||
pstmt2.setDate(4,new Date(System.currentTimeMillis()));
|
||||
pstmt2.setInt(5, 1);
|
||||
pstmt2.setInt(6,1);
|
||||
pstmt2.addBatch();
|
||||
|
||||
pstmt2.setString(1, tableName);
|
||||
pstmt2.setInt(2, 2);
|
||||
pstmt2.setInt(3, 1);
|
||||
pstmt2.setDate(4,new Date(System.currentTimeMillis()));
|
||||
pstmt2.setInt(5, 1);
|
||||
pstmt2.setInt(6,1);
|
||||
pstmt2.addBatch();
|
||||
|
||||
pstmt2.setString(1, tableName);
|
||||
pstmt2.setInt(2, 3);
|
||||
pstmt2.setInt(3, 1);
|
||||
pstmt2.setDate(4,new Date(System.currentTimeMillis()));
|
||||
pstmt2.setInt(5, 1);
|
||||
pstmt2.setInt(6,1);
|
||||
pstmt2.addBatch();
|
||||
}
|
||||
pstmt2.executeBatch();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}finally{
|
||||
try {
|
||||
if(pstmt!=null){
|
||||
pstmt.close();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
if(pstmt2!=null){
|
||||
pstmt2.close();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
conn.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testDruid(){
|
||||
C3p0Plugin c3p0=new C3p0Plugin("jdbc:mysql://localhost:3306/nms_sync","root","root");
|
||||
c3p0.setInitialPoolSize(1);
|
||||
c3p0.setMaxIdleTime(30);
|
||||
c3p0.setMaxPoolSize(2);
|
||||
c3p0.setMinPoolSize(1);
|
||||
ActiveRecordPlugin arp=new ActiveRecordPlugin("c3p0",c3p0);
|
||||
|
||||
c3p0.start();
|
||||
arp.start();
|
||||
|
||||
C3p0Plugin c3p02=new C3p0Plugin("jdbc:mysql://10.0.6.247:3306/nms","nms","nms");
|
||||
c3p02.setInitialPoolSize(1);
|
||||
c3p02.setMaxIdleTime(30);
|
||||
c3p02.setMaxPoolSize(2);
|
||||
c3p02.setMinPoolSize(1);
|
||||
ActiveRecordPlugin arp2=new ActiveRecordPlugin("c3p02",c3p02);
|
||||
|
||||
c3p02.start();
|
||||
arp2.start();
|
||||
|
||||
DruidPlugin druid=new DruidPlugin("jdbc:mysql://10.0.6.247:3306/nms-dev","nms","nms");
|
||||
druid.setInitialSize(1);
|
||||
druid.setMaxActive(2);
|
||||
druid.setMinIdle(1);
|
||||
druid.setMaxWait(60000);
|
||||
ActiveRecordPlugin arp3=new ActiveRecordPlugin("druid",druid);
|
||||
|
||||
druid.start();
|
||||
arp3.start();
|
||||
|
||||
List<Record> find = Db.find("select * from table_sync_info");
|
||||
for (Record record : find) {
|
||||
System.out.println(record);
|
||||
}
|
||||
|
||||
System.out.println("----------------------------");
|
||||
|
||||
List<Record> find2 = Db.use("c3p02").find("select * from node_table");
|
||||
for (Record record : find2) {
|
||||
System.out.println(record);
|
||||
}
|
||||
|
||||
|
||||
System.out.println("----------------------------");
|
||||
List<Record> find3 = Db.use("druid").find("select * from node_table");
|
||||
for (Record record : find3) {
|
||||
System.out.println(record);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDataSize(){
|
||||
DruidPlugin druid=new DruidPlugin("jdbc:mysql://10.0.6.247:3306/nms","nms","nms");
|
||||
druid.setInitialSize(1);
|
||||
druid.setMaxActive(2);
|
||||
druid.setMinIdle(1);
|
||||
druid.setMaxWait(60000);
|
||||
ActiveRecordPlugin arp=new ActiveRecordPlugin(druid);
|
||||
druid.start();
|
||||
arp.start();
|
||||
List<Record> find = Db.find("select count(*) size from detect_info_cpu");
|
||||
System.out.println(find.get(0).getInt("size"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBatchDelete(){
|
||||
PropKit.use("db.properties");
|
||||
DruidPlugin masterDruid=new DruidPlugin(PropKit.get("dburl"),PropKit.get("dbusername"),PropKit.get("dbpassword"));
|
||||
masterDruid.setInitialSize(1);
|
||||
masterDruid.setMaxActive(2);
|
||||
masterDruid.setMinIdle(1);
|
||||
masterDruid.setMaxWait(600000);
|
||||
ActiveRecordPlugin masterArp=new ActiveRecordPlugin("masterDataSource",masterDruid);
|
||||
masterArp.setShowSql(true);
|
||||
masterDruid.start();
|
||||
masterArp.start();
|
||||
List<Integer> ids=new ArrayList<Integer>();
|
||||
ids.add(100026);
|
||||
ids.add(100027);
|
||||
List<Record> find = Db.find("select * from xt_yh_js_index where id in (?,?)",ids.toArray());
|
||||
System.out.println(JSON.toJSON(find));
|
||||
Object[] array = ids.toArray();
|
||||
System.out.println(array);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testICallBack(){
|
||||
DruidPlugin druid=new DruidPlugin("jdbc:mysql://10.0.6.247:3306/nms-synctest2","nms","nms");
|
||||
druid.setInitialSize(1);
|
||||
druid.setMaxActive(2);
|
||||
druid.setMinIdle(1);
|
||||
druid.setMaxWait(60000);
|
||||
ActiveRecordPlugin arp3=new ActiveRecordPlugin("druid",druid);
|
||||
|
||||
druid.start();
|
||||
arp3.start();
|
||||
Db.execute(new ICallback(){
|
||||
@Override
|
||||
public Object call(java.sql.Connection conn) throws SQLException {
|
||||
CallableStatement proc=null;
|
||||
try{
|
||||
proc=conn.prepareCall("{call pro_createTable(?,?,?)}");
|
||||
proc.setString(1,"di_thtest");
|
||||
proc.setString(2,"age bigint,name varchar(11)");
|
||||
proc.setString(3, "data_check_time:seq_id:detection_set_info_id:");
|
||||
proc.execute();
|
||||
} catch (Exception e){
|
||||
e.printStackTrace();
|
||||
} finally{
|
||||
if(conn!=null){
|
||||
conn.close();
|
||||
}
|
||||
if(proc!=null){
|
||||
proc.close();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void addEventRecordLibraryData(){
|
||||
PropKit.use("db.properties");
|
||||
DruidPlugin masterDruid=new DruidPlugin(PropKit.get("dburl"),PropKit.get("dbusername"),PropKit.get("dbpassword"));
|
||||
masterDruid.setInitialSize(1);
|
||||
masterDruid.setMaxActive(2);
|
||||
masterDruid.setMinIdle(1);
|
||||
masterDruid.setMaxWait(600000);
|
||||
ActiveRecordPlugin masterArp=new ActiveRecordPlugin("masterDataSource",masterDruid);
|
||||
masterArp.setShowSql(true);
|
||||
masterDruid.start();
|
||||
masterArp.start();
|
||||
|
||||
List<Record> find = Db.find("select * from event_record_library");
|
||||
List<Record> datas=new ArrayList<Record>();
|
||||
for(Record data:find){
|
||||
Record record=new Record();
|
||||
record.set("table_name", "event_record_library");
|
||||
record.set("event", 1);
|
||||
record.set("target_id", data.getLong("id"));
|
||||
datas.add(record);
|
||||
}
|
||||
Db.batchSave("table_event_log", datas, 500);
|
||||
}
|
||||
}
|
||||
32
nms_sync/src/com/nms/test/TestExecutors.java
Normal file
32
nms_sync/src/com/nms/test/TestExecutors.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package com.nms.test;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class TestExecutors {
|
||||
@Test
|
||||
public void test1(){
|
||||
ExecutorService service = Executors.newFixedThreadPool(5);
|
||||
for(int i=0;i<6;i++){
|
||||
service.execute(new TestThread(i));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class TestThread implements Runnable{
|
||||
private int index;
|
||||
public TestThread(int index){
|
||||
this.index=index;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(){
|
||||
if(index==3){
|
||||
throw new RuntimeException("error");
|
||||
}
|
||||
System.out.println("test"+index);
|
||||
}
|
||||
}
|
||||
}
|
||||
37
nms_sync/src/com/nms/test/TestThread.java
Normal file
37
nms_sync/src/com/nms/test/TestThread.java
Normal file
@@ -0,0 +1,37 @@
|
||||
package com.nms.test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
|
||||
import com.jfinal.plugin.activerecord.Db;
|
||||
import com.jfinal.plugin.activerecord.Record;
|
||||
import com.jfinal.plugin.druid.DruidPlugin;
|
||||
|
||||
public class TestThread implements Runnable{
|
||||
static{
|
||||
DruidPlugin druid=new DruidPlugin("jdbc:mysql://localhost:3306/nms_sync","nms","nms");
|
||||
druid.setInitialSize(1);
|
||||
druid.setMaxActive(2);
|
||||
druid.setMinIdle(1);
|
||||
druid.setMaxWait(60000);
|
||||
ActiveRecordPlugin arp=new ActiveRecordPlugin(druid);
|
||||
druid.start();
|
||||
arp.start();
|
||||
}
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("进入线程任务");
|
||||
Record find = Db.findFirst(" select * from table_sync_info where id =1 ");
|
||||
System.out.println(JSON.toJSON(find));
|
||||
System.out.println("线程任务结束");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
ScheduledExecutorService service = Executors.newScheduledThreadPool(1);
|
||||
service.scheduleWithFixedDelay(new TestThread(), 0, 5000, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user