2016-09-30 21 views
1

我有一個使用spring 3開發的可執行jar。它使用@Scheduled註釋定期執行一些任務並生成數據,主要是計數器。現在我想顯示這些計數器用於監控和分析目的,類似於彈簧引導提供的here如何爲可執行jar添加自定義端點

/** 
* 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類:

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

嘗試,因爲它需要彈簧4,我的罐子具有相關性的使用彈簧3

這裏是我的@Configuration I類不能使用彈簧啓動與@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; 
} 

} 

這沒有奏效。

有什麼辦法可以在沒有彈簧啓動的情況下擁有類似的端點嗎?或者我如何顯示這些計數器?可以使用嵌入式碼頭來達到目的嗎?

謝謝。

+0

你不能自己編碼嗎?根據你寫的內容,我知道這些生成的計數器保持不變,直到另一個計劃的作業結束爲止。如果是這樣,則創建一個可以像這些值的存儲庫一樣運行的bean。然後添加一個控制器,該控制器將從這個bean-repository中讀取最新值並以application/json格式顯示它們。完成。 –

+0

感謝您提供這種方法。我其實用@Controller試過。編輯帖子以添加控制器代碼。但是,這並沒有奏效。我想因爲我只有一個罐子。沒有嵌入式Web容器。這是正確的嗎?或者我錯過了什麼?如果它是一個基本問題,我很抱歉。我是春季MVC的新手。 – pooja

回答

0

我想通了。嵌入碼頭可以輕鬆解決問題。重構我的代碼一點點,就從主類分離配置類,並從主開始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類將加載應用程序上下文和配置類以及se依次加載項目中指定的組件,它將在通過命令行提供的端口上運行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的罐子{的JARname}的.jar 該控制器將顯示'status'對象以json格式打出時: localhost:8080/status

相關問題