我有一個使用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;
}
}
這沒有奏效。
有什麼辦法可以在沒有彈簧啓動的情況下擁有類似的端點嗎?或者我如何顯示這些計數器?可以使用嵌入式碼頭來達到目的嗎?
謝謝。
你不能自己編碼嗎?根據你寫的內容,我知道這些生成的計數器保持不變,直到另一個計劃的作業結束爲止。如果是這樣,則創建一個可以像這些值的存儲庫一樣運行的bean。然後添加一個控制器,該控制器將從這個bean-repository中讀取最新值並以application/json格式顯示它們。完成。 –
感謝您提供這種方法。我其實用@Controller試過。編輯帖子以添加控制器代碼。但是,這並沒有奏效。我想因爲我只有一個罐子。沒有嵌入式Web容器。這是正確的嗎?或者我錯過了什麼?如果它是一個基本問題,我很抱歉。我是春季MVC的新手。 – pooja