Java tutorial
/** * Copyright (C) @2014 Webank Group Holding Limited * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package cn.webank.ecif.index.server; import java.util.Arrays; import java.util.Properties; import java.util.concurrent.ExecutorService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.ReloadableResourceBundleMessageSource; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import cn.webank.ecif.index.async.EcifThreadFactory; import cn.webank.framework.biz.async.MessageListener; import cn.webank.framework.biz.server.WeBankBaseServer; import cn.webank.framework.biz.service.support.WeBankServiceDispatcher; import cn.webank.framework.message.SolaceManager; /** * @author jonyang * */ public class EcifIndexServer extends WeBankBaseServer { private final static Logger LOG = LoggerFactory.getLogger(EcifIndexServer.class); /** * @param args */ public static void main(String[] args) { final EcifIndexServer server = new EcifIndexServer(); // start spring context; final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(); context.getEnvironment().setActiveProfiles("product"); context.setConfigLocation("application.xml"); context.refresh(); context.start(); // thread pool construct ThreadPoolTaskExecutor taskThreadPool = context.getBean("taskExecutor", ThreadPoolTaskExecutor.class); server.setTaskThreadPool(taskThreadPool); ExecutorService fixedThreadPool = context.getBean("fixedTaskExecutor", ExecutorService.class); server.setSchedulerThreadPool(fixedThreadPool); EcifThreadFactory threadFactory = context.getBean("cn.webank.ecif.index.async.EcifThreadFactory", EcifThreadFactory.class); server.setThreadFactory(threadFactory); Properties props = context.getBean("ecifProperties", java.util.Properties.class); WeBankServiceDispatcher serviceDispatcher = context.getBean( "cn.webank.framework.biz.service.support.WeBankServiceDispatcher", WeBankServiceDispatcher.class); ReloadableResourceBundleMessageSource bundleMessageSource = context.getBean("messageSource", ReloadableResourceBundleMessageSource.class); String topics = props.getProperty("listener.topics"); String[] splits = topics.split(","); SolaceManager solaceManager = context.getBean("cn.webank.framework.message.SolaceManager", SolaceManager.class); MessageListener messageLisener = new MessageListener( // props.getProperty("listener.queue"), Arrays.asList(splits), Integer.parseInt(props.getProperty("listener.scanintervalseconds", "5")), taskThreadPool, Integer.parseInt(props.getProperty("listener.timeoutseconds", "5")), serviceDispatcher, solaceManager, bundleMessageSource); server.addListenerOnSchedule(messageLisener); // register shutdownhook Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { try { // close resource if (context != null) { context.close(); } } catch (Exception e) { LOG.error("shutdown error", e); } } }); // hold server try { LOG.info("ecif-index server start ok!"); server.start(); } catch (Exception e) { try { // close resource server.shutDown(); if (context != null) { context.close(); } } catch (Exception ex) { LOG.error("shutdown error", ex); } } LOG.info("ecif-index server stop !"); } }