⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 agentprocess.java.svn-base

📁 this is example use EJB with jboss.
💻 SVN-BASE
字号:
/*
 * Copyright(C) 2008, NTT AT Co., Ltd.
 * Project: AWGView
 *
 * Notes:
 *  N/A
 *
 * Record of change:
 * Date         Version      Name       Content
 * 2009/01/10   1.0          TriNT      First create       
 */
package jp.co.ntt.awgview.server.thread;

import java.util.Vector;

import javax.jms.Message;

import jp.co.ntt.awgview.server.common.LogWriter;
import jp.co.ntt.awgview.server.common.Utils;
import jp.co.ntt.awgview.server.constant.Constants;
import jp.co.ntt.awgview.server.jms.JmsSnmpReplier;
import jp.co.ntt.awgview.server.snmp.SnmpManager;
import jp.co.ntt.awgview.server.vo.SnmpVO;

/**
 * Class name 	: AgentProcess <BR>
 * 
 * Package 		: jp.co.ntt_at.awgview.server.thread <BR>
 * 
 * Description	: This class provides execute control execute Snmp Command to a AWG-NE Agent<BR>
 * 
 * @author 		: AI&T
 * @version 	: 1.0
 */
public class AgentProcess implements Runnable {
	
	private SnmpManager snmpManager = null;

	private AgentHelper agentHelper = null;

	private String ipAddress = null;
	
	/** Create object to keep Thread */
	private Thread threadRunCommand;
	
	JmsSnmpReplier jmsSnmpReplier = null;
	
	private static long STEP_THREAD_SLEEP = 10;
	
	/**
	 * AgentProcess Contractor
	 * 
	 * @param ipAddress
	 */
	public AgentProcess(String ipAddress, JmsSnmpReplier jmsSnmpReplier) {
		this.jmsSnmpReplier = jmsSnmpReplier;
		this.ipAddress = ipAddress;
		snmpManager = new SnmpManager(false);
		agentHelper = new AgentHelper();
	}

	/**
	 * Start execute Snmp Command sent to AWG-NE
	 */
	public void start() {
		threadRunCommand = new Thread(this, ipAddress);
		threadRunCommand.start();
	}
	
	/**
	 * Run thread to execute SNMP Command to an AWG-NE Agent and using JMS module send response
	 */
	public void run() {
		while (true) {
			try {				
				waitWhileEmpty();							
				execute();
			} catch (Exception e) {
				LogWriter.getSNMPLogger().error(e.toString());
				if(LogWriter.getSNMPLogger().isDebugEnabled()){
					LogWriter.getSNMPLogger().debug(Utils.parseException(e));
				}
			}
		}
	}	
	
	/**
	 * Add data to send and reply Requestor
	 * 
	 * @param snmpVO
	 * @param jmsSnmpReplier
	 * @param message
	 */
	public void addData(JmsSnmpReplier jmsSnmpReplier, Message message, SnmpVO snmpVO) {
		this.jmsSnmpReplier = jmsSnmpReplier;
		this.agentHelper.addData(message, snmpVO);
	}

	/**
	 * Execute request from Requestor
	 */
	private void execute() {
		try{
			if (!agentHelper.isEmpty()) {
				Vector<Message> tempMessage = new Vector<Message>();
				Vector<SnmpVO> tempSnmpVO = new Vector<SnmpVO>();
				
				int len = agentHelper.size();
				Vector<SnmpVO> lstSnmpVO = agentHelper.getCurrSnmpVOList();
				Vector<Message> lstMessage = agentHelper.getCurrMessageList();
				
				LogWriter.getSNMPLogger().debug("============================================================");
				LogWriter.getSNMPLogger().debug("-----------------------------------------");
				LogWriter.getSNMPLogger().debug("Start execute snmp command >> ipAddress: " + ipAddress + " >> number command: " + len);
				LogWriter.getSNMPLogger().debug(">> lstSnmpVO.size : " + lstSnmpVO.size());
				LogWriter.getSNMPLogger().debug(">> lstMessage.size : " + lstMessage.size());
				LogWriter.getSNMPLogger().debug("-----------------------------------------");
				if ((lstSnmpVO != null) && (!lstSnmpVO.isEmpty()) 
						&& (lstMessage != null) && (!lstMessage.isEmpty())){
					for (int i = 0; i < len; i++) {
						try {
							SnmpVO snmpVO = agentHelper.getCurrSnmpVOList().get(i);
							Message message = agentHelper.getCurrMessageList().get(i);
							
							doSnmpCommand(message, snmpVO);
							
							tempMessage.addElement(message);
							tempSnmpVO.addElement(snmpVO);
							
						} catch (Exception e) {
							LogWriter.getSNMPLogger().error(e.toString());
							e.printStackTrace();
						}
					}
				}
				
				if ((tempMessage != null)&& (tempSnmpVO != null)
						&& (!tempMessage.isEmpty()) && (!tempSnmpVO.isEmpty())) {
					agentHelper.removeAll(tempMessage, tempSnmpVO);
				}
								
				LogWriter.getSNMPLogger().debug("-----------------------------------------");
				len = agentHelper.size();					
				LogWriter.getSNMPLogger().debug("End execute snmp command >> ipAddress: " + ipAddress + " >> number command: " + len);
				LogWriter.getSNMPLogger().debug(">> lstSnmpVO.size : " + lstSnmpVO.size());
				LogWriter.getSNMPLogger().debug(">> lstMessage.size : " + lstMessage.size());
				LogWriter.getSNMPLogger().debug("-----------------------------------------");
				LogWriter.getSNMPLogger().debug(AgentDispatcher.printListToDebug());				
				LogWriter.getSNMPLogger().debug("============================================================");
			}
		}catch (Exception e){
			LogWriter.getSNMPLogger().error("Error when execute request from Requestor" + e.toString());
			if(LogWriter.getSNMPLogger().isDebugEnabled()){
				LogWriter.getSNMPLogger().debug(Utils.parseException(e));
			}
		}	
	}

	/**
	 * Do Snmp Command and reply response data to Requestor
	 * 
	 * @param jmsReplier
	 * @param message
	 * @param snmpVO
	 */
	private void doSnmpCommand(Message message,SnmpVO snmpVO) {
		try {
			LogWriter.getSNMPLogger().debug("-----------------------------");
			LogWriter.getSNMPLogger().info("Command sent from client to server with data:");
			LogWriter.getSNMPLogger().info(snmpVO.toString());
			// 2. Send request to PTE
			long startTime1 = System.currentTimeMillis();
			LogWriter.getSNMPLogger().debug("Start send to PTE at " + Utils.getNow(Constants.DATE_FORMAT));
			
			snmpManager.setIPAddress(ipAddress);
			SnmpVO updateSnmpVO = sendSnmpToAWGNE(snmpVO);

			 LogWriter.getSNMPLogger().debug("Received SnmpVO from PTE after: " 
			 + (System.currentTimeMillis() - startTime1) + " millis. At " + Utils.getNow(Constants.DATE_FORMAT));
			 LogWriter.getSNMPLogger().debug("End send at " + Utils.getNow(Constants.DATE_FORMAT));
			 LogWriter.getSNMPLogger().debug("-----------------------------");
			 
			if (updateSnmpVO != null) {
				// 2. Reply client
				//long startTime2 = System.currentTimeMillis();				
				LogWriter.getSNMPLogger().info("Start reply to client " + Utils.getNow(Constants.DATE_FORMAT));
				LogWriter.getSNMPLogger().info("Data send to client: " + updateSnmpVO.toString());
				
				this.jmsSnmpReplier.replyToClient(message, updateSnmpVO);
				
				//LogWriter.getSNMPLogger().debug("Send data to client after: " 
				//	+ (System.currentTimeMillis() - startTime2) + " millis. At " + Utils.getNow(Constants.DATE_FORMAT));
				//LogWriter.getSNMPLogger().debug("End executing command form requested client after: "
				//	+ (System.currentTimeMillis() - startTime1) + " millis. At " + Utils.getNow(Constants.DATE_FORMAT));
			}			
		} catch (Exception e) {
			LogWriter.getSNMPLogger().debug(e.toString());
			if(LogWriter.getSNMPLogger().isDebugEnabled()){
				LogWriter.getSNMPLogger().debug(Utils.parseException(e));
			}			
		}
	}

	/**
	 * Send Snmp command to AWG-NE
	 * 
	 * @param SnmpVO
	 *            : Object Snmp command
	 */
	private SnmpVO sendSnmpToAWGNE(SnmpVO snmpVO) {
		try {
			SnmpVO updateSnmpVO = new SnmpVO();

			if (snmpVO.getPduType() == Constants.SNMP_SET) { // SET
				LogWriter.getSNMPLogger().debug("Command type: SNMP SET >>ipAddress: " + this.ipAddress);
				if ((snmpVO != null)) {
					if (snmpManager == null) {
						snmpManager = new SnmpManager(false);
					}
					updateSnmpVO = snmpManager.snmpSetSet(snmpVO);
				}
			} else if (snmpVO.getPduType() == Constants.SNMP_GET) { // GET
				LogWriter.getSNMPLogger().debug("Command type: SNMP GET >>ipAddress: " + this.ipAddress);
				if ((snmpVO != null)) {
					if (snmpManager == null) {
						snmpManager = new SnmpManager(false);
					}
					updateSnmpVO = snmpManager.snmpSetGet(snmpVO);
				}
			}

			if (updateSnmpVO == null) {
				LogWriter.getSNMPLogger().debug(
						"Data from PTE send to client: null");
				return null;
			}
			return updateSnmpVO;
		} catch (Exception e) {
			LogWriter.getSNMPLogger().error(e.toString());
			if (LogWriter.getSNMPLogger().isTraceEnabled()) {
				LogWriter.getSNMPLogger().trace(Utils.parseException(e));
			}			
			return null;
		}
	}
	
	/**
	 * Sets the IP Address
	 * 
	 * @param ipAddress
	 */
	public void setIPAddress(String ipAddress) {
		this.ipAddress = ipAddress;
	}
	
	/**
	 * Returns the IP Address
	 * 
	 * @return ipAddress
	 */
	public String getIPAddress() {
		return this.ipAddress;
	}
	
	/**
	 * Sets the SnmpManager
	 * 
	 * @param snmpManager
	 */
	public void setSnmpManager(SnmpManager snmpManager) {
		this.snmpManager = snmpManager;
	}
	
	/**
	 * Returns the SnmpManager Object
	 * @return SnmpManager
	 */
	public SnmpManager getSnmpManager() {
		return this.snmpManager;
	}
		
	/**
	 * Number command request
	 * @return int
	 */
	public int size() {
		if (agentHelper == null){
			agentHelper = new AgentHelper();			
		}
		return this.agentHelper.size();
	}	

	/**
	 * isEmpty
	 * 
	 * @return boolean
	 */
	private boolean isEmpty() {
		if (agentHelper == null){
			agentHelper = new AgentHelper();			
		}
		return (this.agentHelper.size() == 0);
	}
	
	/**
	 * Do destroy
	 */
	public void destroy() {
		try {
			if (snmpManager != null) {
				snmpManager.destroy();
			}
			
			if (agentHelper != null) {
				agentHelper.destroy();
			}			
			
			if (threadRunCommand.isAlive()) {
				threadRunCommand.interrupt();
			}
									
		} catch (Exception e) {
			LogWriter.getSNMPLogger().warn(e.toString());			
			if(LogWriter.getSNMPLogger().isDebugEnabled()){
				LogWriter.getSNMPLogger().debug(Utils.parseException(e));
			}
		}
	}
	
	/**
	 * waitWhileEmpty
	 * 
	 * @throws InterruptedException
	 */
	public synchronized void waitWhileEmpty() throws InterruptedException {
		while (isEmpty()) {
			Thread.sleep(STEP_THREAD_SLEEP);
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -