如何为可执行jar添加自定义端点

时间:2016-09-30 18:59:57

标签: java spring

我有一个使用spring 3开发的可执行jar。它使用@Scheduled注释定期执行一些任务并生成数据,主要是计数器。现在我想显示这些计数器以进行监视和分析,类似于spring boot提供的here。我不能使用弹簧靴,因为它需要弹簧4而我的jar具有使用弹簧3的依赖性。

这是我的@configuration类:

 /**
 * Requester - The main entry point for this application.
 *
 */
@Configuration
@ComponentScan(basePackages = "com.tpv.req")
@EnableScheduling
@ImportResource({ "classpath:/spring/applicationContext-common.xml" })
@PropertySource(value="file:/opt/requester/requester.properties")
public class Requester implements SchedulingConfigurer {

protected static final Logger logger = LoggerFactory.getLogger(Requester.class);

@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
    taskRegistrar.setScheduler(taskExecutor());
}

@Bean(destroyMethod = "shutdown")
public Executor taskExecutor() {
    return Executors.newScheduledThreadPool(1);
}

@Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
    PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
    return pspc;
}

@SuppressWarnings({ "unused", "resource" })
public static void main(String args[]) {
    AbstractApplicationContext context = new AnnotationConfigApplicationContext(Requester.class);
}

}

@Component class:

@Component
public class CustomRequester {
@Scheduled(initialDelay = 5000, fixedDelayString = "${requester.wait.time}")
public void processRequests() {
      //Perform some task
}

尝试使用@Controller:

@Controller
@RequestMapping("/status")
public class StatusController {

@Autowired
Status status;


/**
 * @return Status object (as json)
 */
@RequestMapping(method=RequestMethod.GET)
public @ResponseBody Status doResponse()  {
    return status;
}

}

这不起作用。

有没有办法在没有弹簧启动的情况下拥有类似的端点?或者我该如何显示这些计数器?可以使用嵌入式码头服务吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

我明白了。嵌入码头可以轻松解决问题。在将配置类与主类分离并从main启动jetty服务器方面,重构了我的代码。 这是代码:

public class ScannerStartup {

private static Logger logger = LoggerFactory.getLogger(ScannerStartup.class);
private static final int DEFAULT_PORT = 8080;
private static final String CONTEXT_PATH = "/";
// Package of the config class
private static final String CONFIG_LOCATION = "com.tpv.req.config";
private static final String MAPPING_URL = "/*";

public static void main(String args[]) throws Exception {
    startJetty(getPortFromArgs(args));
}

private static int getPortFromArgs(String[] args) {
    if (args.length > 0) {
        try {
            return Integer.valueOf(args[0]);
        } catch (NumberFormatException ignore) {
        }
    }
    logger.debug("No server port configured, falling back to {}", DEFAULT_PORT);
    return DEFAULT_PORT;
}

private static void startJetty(int port) throws Exception {
    Server server = new Server(port);
    server.setHandler(getServletContextHandler(getContext()));
    server.start();
    logger.info("Server started at port {}", port);
    server.join();
}

private static ServletContextHandler getServletContextHandler(WebApplicationContext context) throws IOException {
    ServletContextHandler contextHandler = new ServletContextHandler();
    contextHandler.setErrorHandler(null);
    contextHandler.setContextPath(CONTEXT_PATH);
    contextHandler.addServlet(new ServletHolder(new DispatcherServlet(context)), MAPPING_URL);
    contextHandler.addEventListener(new ContextLoaderListener(context));
    return contextHandler;
}

private static WebApplicationContext getContext() {
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.setConfigLocation(CONFIG_LOCATION);
    return context;
}

}

配置类:我将它们分离为AppConfig和WebConfig

@Configuration
@ComponentScan(basePackages = "com.tpv.req")
@EnableScheduling
@PropertySource(value = "file:/opt/scanner-application.properties")
public class AppConfig implements SchedulingConfigurer {

protected static final Logger logger = LoggerFactory.getLogger(AppConfig.class);

@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
    taskRegistrar.setScheduler(taskExecutor());
}

@Bean(destroyMethod = "shutdown")
public Executor taskExecutor() {
    return Executors.newScheduledThreadPool(1);
}

@Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
    PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
    return pspc;
}
}

WebMvcConfig类:

@Configuration
@ComponentScan(basePackages = "com.tpv.req.controller")
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {`enter code here`
        final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        final ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
        converter.setObjectMapper(objectMapper);
        converters.add(converter);
        super.configureMessageConverters(converters);
    }

}

ScannerStartup类中的主要方法将加载应用程序上下文和配置类,然后这些将加载项目中指定的组件,它将在通过命令行提供的端口上运行jetty服务器。如果没有提供,它将使用默认端口8080。

以下是控制器类的示例:

@Controller
@RequestMapping("/status")
public class ScannerStatusController {

    @Autowired
    ScannerStatus status;

    /**
     * @return Status object (as json)
     */
    @RequestMapping(method=RequestMethod.GET)
    public @ResponseBody ScannerStatus doResponse()  {
        return status;
    }

}

启动应用程序: java -jar {jarname} .jar 该控制器将显示&#39; status&#39;点击时以json格式的对象: 本地主机:8080 /状态