在非WEB环境中关闭 java 进程 及 spring 容器

static AbstractApplicationContext ctx = new FileSystemXmlApplicationContext("classpath:context.xml");
static boolean isDo = true;

public static void main(String[] args) throws InterruptedException {
	for(int i=0; i<10; i++){
		new Producer().start();
	}
	ctx.registerShutdownHook();
	Runtime.getRuntime().addShutdownHook(new Thread() {
		public void run() {
			try {
				isDo = false;
				Thread.sleep(1000);
				//ctx.close();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(ctx.getBean("messageListener", TestAmqSpringMessageListener.class).getCounter());
		}
	});
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注