1. 定义队列
2. 入队操作
3. 操作队列
public static Vector<ReceiveData> queueain = new Vector<ReceiveData>();
2. 入队操作
package cn.com.superv.emanager.dnsalarm;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.superv.emanager.base.bo.Config;
import cn.com.superv.emanager.manager.ConfigManager;
public class ReceiveFromGW extends Thread{
private static final Log log = LogFactory.getLog(ReceiveFromGW.class);
private static final int maxreceivelength = 10240;
private static int remoteport = 2000; //这个端口从AM_CONFIG读取
private static int maxreceive = 10000; // 能接收的最大消息数
private static DatagramSocket socket=null;
public static boolean bRun = true;
private DatagramPacket inputdp = new DatagramPacket(new byte[maxreceivelength], maxreceivelength);
private ConfigManager configManager;
public void setConfigManager(ConfigManager configManager) {
this.configManager = configManager;
}
public ReceiveFromGW() {
super();
}
public void startReceiveFromGW(){
Config config = configManager.getConfig("ALARM_PORT");
if(config != null){
remoteport = Integer.parseInt(config.getValue().trim());
}
config = configManager.getConifg("ALARM_MAX_RECEIVE");
if(config != null){
maxreceive = Integer.parseInt(config.getValue().trim());
}
getDatagramSocket();
if(socket!=null)
start();
log.info("ReceiveFromGW7750 Start Port["+remoteport+"]");
}
public static DatagramSocket getDatagramSocket()
{
try {
if(socket==null){
socket= new DatagramSocket(remoteport);//绑定端口号
socket.setSendBufferSize(socket.getSendBufferSize()*10);
socket.setReceiveBufferSize(socket.getReceiveBufferSize()*10);
}
}
catch (SocketException e) {
e.printStackTrace();
log.error(e,e.fillInStackTrace());
}
return socket;
}
public void run() {
while(bRun){
try{
if(ReceiveAlarm.bAdd){
if(log.isDebugEnabled())
log.debug("receive gw7750 message port["+remoteport+"]start.............!");
socket.receive(inputdp);
int numBytes = inputdp.getLength();
byte[] readbytes = new byte[numBytes];
System.arraycopy(inputdp.getData(), 0, readbytes, 0, numBytes);
ReceiveData receivedata = new ReceiveData(inputdp.getAddress().getHostAddress(),new String(readbytes));
ReceiveAlarm.addData(receivedata,maxreceive);
log.info("data from port["+remoteport+"] ip:"+receivedata.getIpstr() + " data:" + receivedata.getDatastr());
if(log.isDebugEnabled()){
log.debug("receive gw7750 message end.............!");
}
}
} catch (Exception e) {
e.printStackTrace();
log.error(e,e.fillInStackTrace());
}
}
}
}
3. 操作队列
private final static int MAX_PACKET_LENGTH = 4096;
public static boolean bRun = true;
public static boolean bAdd = true; // 是否继续接收告警
public static Vector<ReceiveData> queueain = new Vector<ReceiveData>();
private DatagramSocket socket=null;
private DatagramPacket outputdp;
private DatagramPacket inputdp = new DatagramPacket(new byte[MAX_PACKET_LENGTH], MAX_PACKET_LENGTH);
/**
* 以队列的方式接收和发送告警
* @param receivedata 接收到的数据
*/
public void receiveAlarm(ReceiveData receivedata){
try {
updateAlarmData(receivedata.getIpstr(),receivedata.getDatastr());
} catch (Exception e) {
log.error(e,e.fillInStackTrace());
}
}
public void startReceiveAlarm(){
start();
log.info("ReceiveAlarm Start!");
}
public void run() {
while(bRun){
try{
synchronized (queueain) {
if(queueain.isEmpty()){
try {
log.info("wait ................................................");
bAdd = true;
queueain.wait(1000 * 300);
} catch (Exception e) {
e.printStackTrace();
log.error(e,e.fillInStackTrace());
}
}
}
while (!queueain.isEmpty()) {
try{
ReceiveData rd = (ReceiveData)queueain.firstElement();
queueain.removeElementAt(0);
log.info("send .......................................... size: "+queueain.size());
receiveAlarm(rd);
} catch (Exception e) {
e.printStackTrace();
log.error(e,e.fillInStackTrace());
}
}
} catch(Exception ex){
log.error(ex,ex.fillInStackTrace());
}
}
}
public static void addData(ReceiveData receivedata, int maxreceive){
synchronized(queueain){
if(queueain.size() > maxreceive){
bAdd = false;
}
if(queueain.size() == 0){
bAdd = true;
}
if(bAdd){
queueain.add(receivedata);
queueain.notifyAll();
}
}
}