Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何为可执行jar添加自定义端点_Java_Spring - Fatal编程技术网

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

Java 如何为可执行jar添加自定义端点,java,spring,Java,Spring,我有一个使用Spring3开发的可执行jar。它使用@Scheduled注释定期执行一些任务,并生成数据,主要是计数器。现在,我想显示这些计数器用于监视和分析目的,类似于SpringBoot提供的功能。我不能使用SpringBoot,因为它需要Spring4,而我的jar具有使用Spring3的依赖项 这是我的@configuration类: /** * Requester - The main entry point for this application. * */ @Config

我有一个使用Spring3开发的可执行jar。它使用@Scheduled注释定期执行一些任务,并生成数据,主要是计数器。现在,我想显示这些计数器用于监视和分析目的,类似于SpringBoot提供的功能。我不能使用SpringBoot,因为它需要Spring4,而我的jar具有使用Spring3的依赖项

这是我的@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
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;
}

}
这不起作用

有没有办法在没有spring boot的情况下拥有类似的端点?或者如何显示这些计数器?使用嵌入式码头能达到这个目的吗


多谢各位

我明白了。嵌入jetty可以很容易地解决这个问题。对我的代码进行了一点重构,将配置类与主类分离,并从主类启动jetty server。 代码如下:

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

}
只需使用以下内容启动应用程序: java-jar{jarname}.jar 当您点击以下按钮时,此控制器将以json格式显示“状态”对象:
localhost:8080/status

我猜出来了。嵌入jetty可以很容易地解决这个问题。对我的代码进行了一点重构,将配置类与主类分离,并从主类启动jetty server。 代码如下:

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

}
只需使用以下内容启动应用程序: java-jar{jarname}.jar 当您点击以下按钮时,此控制器将以json格式显示“状态”对象:
localhost:8080/status

您不能自己编写代码吗?根据您所写的内容,我了解到这些生成的计数器在另一个计划作业结束之前保持不变?如果是这样的话,创建一个bean,其行为类似于这些值的存储库。然后添加一个控制器,该控制器将从这个bean存储库中读取最新的值,并以application/json格式显示它们。完成。感谢您提出这种方法。实际上我试过使用@Controller。编辑文章以添加带有控制器的代码。然而,这并没有起作用。我想是因为我只有一个罐子。没有嵌入式web容器。是这样吗?还是我遗漏了什么?如果这是一个基本问题,我很抱歉。我是SpringMVC的新手。你不能自己编写吗?根据您所写的内容,我了解到这些生成的计数器在另一个计划作业结束之前保持不变?如果是这样的话,创建一个bean,其行为类似于这些值的存储库。然后添加一个控制器,该控制器将从这个bean存储库中读取最新的值,并以application/json格式显示它们。完成。感谢您提出这种方法。实际上我试过使用@Controller。编辑文章以添加带有控制器的代码。然而,这并没有起作用。我想是因为我只有一个罐子。没有嵌入式web容器。是这样吗?还是我遗漏了什么?如果这是一个基本问题,我很抱歉。我是SpringMVC的新手。