📄 dobackobject.java
字号:
package com.wireless.sms.unsoap.workthread;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import org.apache.log4j.Logger;
import com.wireless.sms.pub.mq.ObjectQueue;
import com.wireless.sms.unsoap.global.Constant;
public class DoBackObject {
private Logger log = null;
//程序SAVESUM、LOADSUM相同才有正确结果。
public static int SAVESUM = 1000; //每个文件保存对象个数
public static int LOADSUM = 1000; //每载入一次对象个数
// private static String filename[] = {"mo.dump", "linkid.dump",
// "gatewaystate.dump"};
public DoBackObject() {
}
public void setLog(Logger log) {
this.log = log;
}
public void setSAVESUM(int aSAVESUM) {
SAVESUM = aSAVESUM;
}
public void setLOADSUM(int aLOADSUM) {
LOADSUM = aLOADSUM;
}
public synchronized void saveflowObj(ObjectQueue objqueue, String filepath,
String filename) {
if (log != null) log.info("start objoverflow at queue num>>>>" +
objqueue.size());
ObjectOutputStream statos = null;
if (objqueue.size() == 0)
return;
try {
File statfile = this.getNewFile(filepath, filename);
statos = new ObjectOutputStream(new FileOutputStream(
statfile));
Object obj = objqueue.removeNoWait();
for (int i = 0; i < SAVESUM && obj != null; i++) {
statos.writeObject(obj);
statos.flush();
if (log != null) log.info("flow obj :" + obj.toString());
if (i < SAVESUM - 1)
obj = objqueue.removeNoWait();
}
if (statos != null) {
statos.close();
statos = null;
}
}
catch (IOException ex) {
ex.printStackTrace();
if (log != null) log.error(ex.getMessage());
}
finally {
if (statos != null) {
try {
statos.close();
statos = null;
}
catch (IOException ex1) {
}
}
if (log != null) log.info(" objoverflow ended at queue num>>>>" +
objqueue.size());
}
}
/**
* 写多个 obj 到文件,以备以后发送
*
*/
public synchronized ObjectQueue loadflowObj(ObjectQueue objqueue,
String filepath, String filename) {
ObjectInputStream o = null;
try {
if (log != null) log.info("load objoverflow start at queue num>>>>" +
objqueue.size());
File[] objfiles = new File(filepath).listFiles();
if (objfiles != null && objfiles.length > 0) {
for (int i = 0, j = 0; i < LOADSUM && j < objfiles.length; j++) {
//判断是否为要上载的文件
if (objfiles[j].getName().endsWith(filename)) {
o = new ObjectInputStream(new FileInputStream(
objfiles[j]));
Object obj = null;
while (true) {
try {
obj = o.readObject();
i++;
}
catch (Exception ex) {
ex.printStackTrace();
break;
}
if (obj == null)
break;
else
if (log != null) log.info("load obj :" + obj.toString());
objqueue.add(obj);
}
o.close();
o = null;
objfiles[j].delete();
}
}
}
}
catch (IOException ex1) {
ex1.printStackTrace();
if (log != null) log.error(ex1.getMessage());
}
finally {
if (o != null) {
try {
o.close();
o = null;
}
catch (IOException ex1) {
}
}
if (log != null) log.info("load objoverflow start at queue num>>>>" +
objqueue.size());
}
return objqueue;
}
/**
* 从备份mo文件读到内存,以供发送
*
*/
public synchronized void loadAllMO( String filepath,
String filename) {
ObjectInputStream o = null;
try {
if (log != null) log.info("load objoverflow start at queue num>>>>" +
Constant.MOQUEUE.size());
File[] objfiles = new File(filepath).listFiles();
if (objfiles != null && objfiles.length > 0) {
for (int i = 0; i < objfiles.length; i++) {
if (objfiles[i].getName().endsWith(filename)) {
o = new ObjectInputStream(new FileInputStream(
objfiles[i]));
while (true) {
Object obj = null;
try {
obj = o.readObject();
}
catch (Exception ex) {
break;
}
if (obj == null)
break;
else
if (log != null) log.info("load obj :" + obj.toString());
Constant.MOQUEUE.add(obj);
}
o.close();
o = null;
objfiles[i].delete();
}
}
}
}
catch (IOException ex1) {
ex1.printStackTrace();
if (log != null) log.error(ex1.getMessage());
}
finally {
if (o != null) {
try {
o.close();
o = null;
}
catch (IOException ex1) {
}
}
if (log != null) log.info("load objoverflow start at queue num>>>>" +
Constant.MOQUEUE.size());
}
}
/**
* 从备份mt文件读到内存,以供发送
*
*/
public synchronized void loadAllMT( String filepath,
String filename) {
ObjectInputStream o = null;
try {
if (log != null) log.info("load objoverflow start at queue num>>>>" +
Constant.MOQUEUE.size());
File[] objfiles = new File(filepath).listFiles();
if (objfiles != null && objfiles.length > 0) {
for (int i = 0; i < objfiles.length; i++) {
if (objfiles[i].getName().endsWith(filename)) {
o = new ObjectInputStream(new FileInputStream(
objfiles[i]));
while (true) {
Object obj = null;
try {
obj = o.readObject();
}
catch (Exception ex) {
break;
}
if (obj == null)
break;
else
if (log != null) log.info("load obj :" + obj.toString());
Constant.MTQUEUE.add(obj);
}
o.close();
o = null;
objfiles[i].delete();
}
}
}
}
catch (IOException ex1) {
ex1.printStackTrace();
if (log != null) log.error(ex1.getMessage());
}
finally {
if (o != null) {
try {
o.close();
o = null;
}
catch (IOException ex1) {
}
}
if (log != null) log.info("load objoverflow start at queue num>>>>" +
Constant.MTQUEUE.size());
}
}
/**
* 从备份 obj 文件读到内存,以供发送
*/
public synchronized Object loadObj(String filepath, String filename) {
ObjectInputStream o = null;
Object obj = null;
try {
File[] objfiles = new File(filepath).listFiles();
if (objfiles != null && objfiles.length > 0) {
for (int j = 0; j < objfiles.length; j++) {
o = new ObjectInputStream(new FileInputStream(
objfiles[j]));
if (objfiles[j].getName().endsWith(filename)) {
try {
obj = o.readObject();
}
catch (Exception e) {
break;
}
}
o.close();
objfiles[j].delete();
}
}
}
catch (IOException ex1) {
ex1.printStackTrace();
if (log != null) log.error(ex1.getMessage());
}
finally {
if (o != null) {
try {
o.close();
o = null;
}
catch (IOException ex1) {
}
}
}
return obj;
}
/**
* 写全部 obj 到文件,以备以后发送
*
*/
public synchronized void saveAllObj( String filepath,
String filename) {
ObjectOutputStream statos = null;
try {
File statfile = this.getNewFile(filepath, filename);
statos = new ObjectOutputStream(new FileOutputStream(
statfile));
Object obj = null;
int j = Constant.MOQUEUE.size();
for (int i = 0; i < j; i++) {
obj = Constant.MOQUEUE.removeNoWait();
statos.writeObject(obj);
statos.flush();
}
statos.close();
statos = null;
if (log != null) log.info("flow obj :" + obj.toString());
}
catch (IOException ex) {
ex.printStackTrace();
if (log != null) log.error(ex.getMessage());
}
finally {
if (statos != null) {
try {
statos.close();
statos = null;
}
catch (IOException ex1) {
}
}
}
}
/**
* 写一个 obj 到文件,以备以后发送
*
*/
public synchronized void saveObj(Object obj, String filepath, String filename) {
ObjectOutputStream statos = null;
try {
File statfile = this.getNewFile(filepath, filename);
statos = new ObjectOutputStream(new FileOutputStream(
statfile));
statos.writeObject(obj);
statos.flush();
statos.close();
statos = null;
if (log != null) log.info("flow obj :" + obj.toString());
}
catch (IOException ex) {
ex.printStackTrace();
if (log != null) log.error(ex.getMessage());
}
finally {
if (statos != null) {
try {
statos.close();
statos = null;
}
catch (IOException ex1) {
}
}
}
}
/**
* 获得最新文件对象,如已存在则循环最终返回未存在文件对象
* @param flag int
* @return File
*/
private synchronized File getNewFile(String filepath, String filename) {
String fineName = "";
fineName = filepath + File.separator +
System.currentTimeMillis() + filename;
File f = new File(fineName);
while (f.exists()) {
try {
Thread.sleep(2);
}
catch (InterruptedException ex) {
}
fineName = filepath + File.separator +
System.currentTimeMillis() + filename;
f = new File(fineName);
}
return f;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -