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

📄 emppservice.java

📁 移动empp网关
💻 JAVA
字号:
package cn.madhouse.gateway.empp.service;

import java.io.IOException;
import java.util.*;

import javax.jms.JMSException;
import javax.naming.NamingException;

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

import cn.madhouse.gateway.empp.service.GetInThread;
import cn.madhouse.gateway.empp.service.MoThread;
import cn.madhouse.gateway.empp.service.MtThread;
import cn.madhouse.gateway.empp.service.ReportThread;
import cn.madhouse.gateway.empp.service.SendOutThread;
import cn.madhouse.jms.JmsService;

public class EmppService {
	
	private ThreadGroup emppThreadGroup = new ThreadGroup("emppThreadGroup");
	
	private int outSleep;
	private int moSleep;
	private int mtSleep;
	private int reportSleep;
	private MessageQueue msgQueue;
	private Set<EmppSocket> emppSocketSet;
	private JmsService jmsService;
	
	public void setMoSleep(int moSleep) {
		this.moSleep = moSleep;
	}
	public void setMtSleep(int mtSleep) {
		this.mtSleep = mtSleep;
	}
	public void setOutSleep(int outSleep) {
		this.outSleep = outSleep;
	}
	public void setReportSleep(int reportSleep) {
		this.reportSleep = reportSleep;
	}
	public void setMsgQueue(MessageQueue msgQueue) {
		this.msgQueue = msgQueue;
	}
	public void setEmppSocketSet(Set<EmppSocket> emppSocketSet) {
		this.emppSocketSet = emppSocketSet;
	}
	public void setJmsService(JmsService jmsService) {
		this.jmsService = jmsService;
	}

	public void doService() {
		Map<String,EmppSocket> emppSocketMap = new HashMap<String,EmppSocket>();
		
		//getInThread
		ThreadGroup getInThreadGroup = new ThreadGroup(emppThreadGroup, "getInThreadGroup");
		Iterator it = emppSocketSet.iterator();
		while(it.hasNext()) {
			EmppSocket emppSocket = (EmppSocket) it.next();
			Thread getInThread = new Thread(getInThreadGroup, new GetInThread(msgQueue, emppSocket));
			getInThread.setName("getInThread_" + emppSocket.getAccountId());
			getInThread.start();
			System.out.println("getInThread start!!!");
			emppSocketMap.put(emppSocket.getAccountId(), emppSocket);
		}
		
		//sendOutThread
		Thread sendOutService = new Thread(emppThreadGroup, new SendOutThread(outSleep, msgQueue, emppSocketMap));
		sendOutService.setName("sendOutThread");
		sendOutService.start();
		System.out.println("sendOutThread start!!!");
		
		//moThread
		Thread moService = new Thread(emppThreadGroup, new MoThread(moSleep, msgQueue, jmsService));
		moService.setName("moThread");
		moService.start();
		System.out.println("moThread start!!!");
		
		//mtThread
		Thread mtService = new Thread(emppThreadGroup, new MtThread(mtSleep, msgQueue, jmsService));
		mtService.setName("mtThread");
		mtService.start();
		System.out.println("mtThread start!!!");
		
		//reportThread
		Thread reportService = new Thread(emppThreadGroup, new ReportThread(reportSleep, msgQueue, jmsService));
		reportService.setName("reportThread");
		reportService.start();
		System.out.println("reportThread start!!!");
	}

	public void doShutDownWork() {
		Runtime.getRuntime().addShutdownHook(
				new Thread() {
					public void run() {
						try {
							close();
							sleep(10000);
						} catch (IOException e) {
							e.printStackTrace();
						} catch (JMSException e) {
							e.printStackTrace();
						} catch (NamingException e) {
							e.printStackTrace();
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
					private void close() throws IOException, JMSException, NamingException {
						
						//close serviceThread
						emppThreadGroup.destroy();
						System.out.println("all threads are closed!!!");
						
						//close sockets
						Iterator it = emppSocketSet.iterator();
						while(it.hasNext()) {
							EmppSocket emppSocket = (EmppSocket) it.next();
							emppSocket.close();
						}
						System.out.println("all sockets are closed!!!");
						
						//close jms
						jmsService.close();
						System.out.println("jmsService is closed!!!");
						
						//persist msgQueue
						msgQueue.persist();
						System.out.println("EMPP gateway program is going to close in 10 seconds!!!");
					}
				}
		);
	}
	
	
	public static void main(String[] arg) {
		XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("applicationContext-service.xml"));
		EmppService emppService = (EmppService) factory.getBean("emppService");
		emppService.doShutDownWork();
		emppService.doService();
	}
}

⌨️ 快捷键说明

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