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()); } }); }