简体   繁体   English

Websphere崩溃之前,如何在Java中执行特定方法?

[英]How to execute a particular method in java before websphere goes down?

I have a job in java application which is reading data from Oracle database using spring JDBC after every 5 mins. 我在Java应用程序中有一份工作,该工作每5分钟使用Spring JDBC从Oracle数据库读取数据。 The java application is running on WebSphere Application Server. Java应用程序正在WebSphere Application Server上运行。 It loads records with status 'X', after loading records it changes status of records to 'Y'. 它加载状态为“ X”的记录,在加载记录后将其状态更改为“ Y”。 We are reading 10k records at a time and supplying 1k records to each thread within 10 threads for certain Processing. 我们一次读取1万条记录,并为某些处理向10个线程中的每个线程提供1k条记录。

After processing each record, the record state gets changed to 'Z'. 处理完每条记录后,记录状态将变为“ Z”。 Now if something goes wrong while processing records like outOfMemory error and WebSphere goes down, the record state remains 'Y'. 现在,如果在处理诸如outOfMemory错误之类的记录时出现问题并且WebSphere发生故障,则记录状态仍为“ Y”。

So when next time Server starts, the job starts reading records with status 'X'. 因此,下次服务器启动时,作业将开始读取状态为“ X”的记录。 But the records which are unprocessed with status 'Y' will never get loaded now. 但是,状态为“ Y”的未处理记录将永远不会被加载。 So is there any way to call a method while WebSphere goes down? 那么,当WebSphere出现故障时,有什么方法可以调用方法吗? Within which i can write a piece of code to make the status of unprocessed records to 'X', so that they can get picked next time server starts. 我可以在其中编写一段代码,使未处理的记录的状态变为“ X”,以便下次服务器启动时可以选择它们。

If the application has encountered an OutOfMemoryError there is really no reliable way for you to make sure that some code gets executed before going down (actually, the OutOfMemoryError will not actually make the process die by itself, but it doesn't matter - if you're out of memory you can't be sure that you will be able to do anything at all in that process). 如果应用程序遇到了OutOfMemoryError ,实际上没有可靠的方法让您确保在关闭之前执行某些代码(实际上, OutOfMemoryError不会使进程本身死亡,但这无关紧要-如果您“您无法确定自己在该过程中将无法执行任何操作)。

What you should to do is get rid of the 'Y' state. 您应该做的是摆脱“ Y”状态。 Just make sure that the job that reads the items doesn't get to execute more than once (see below). 只要确保读取项目的作业不会多次执行即可(请参见下文)。 You should then be able to just read the items, send them off for processing, and just set the state to 'Z' when you're done (preferably in the same transaction as the processing of each item). 然后,您应该能够读取项目,将其发送出去进行处理,完成后只需将状态设置为“ Z”(最好与处理每个项目的事务相同)。

Now, you don't specify how your job is kicked off every five minutes, so I'm just assuming you're using Spring's scheduling functionality for that. 现在,您无需指定每五分钟开始工作的方式,所以我仅假设您为此使用Spring的调度功能。 If this is the case, the job will never get fired more than once as long as it's still running . 在这种情况下, 只要作业仍在运行 ,就永远不会被解雇。 This means that your job needs to keep track of the items it's sent off and wait for them to finish before exiting. 这意味着您的工作需要跟踪已发送的项目,并等待它们完成后再退出。 This can be done using an ExecutorCompletionService . 这可以使用ExecutorCompletionService完成。 Send each subtask (ie chunk of 1k records) to the same ExecutorCompletionService and poll for finished tasks as long as there are more tasks left. 将每个子任务(即1k条记录的大块)发送到相同的ExecutorCompletionService并轮询剩下的任务,以完成任务。 When all subtasks have returned you can safely exit the parent job. 当所有子任务都返回后,您可以安全地退出父作业。

Another way of doing this (if the 'Y' state is required for some particular reason) would be to check for 'Y' records on startup, eg in a method annotated with @PostConstruct 这样做的另一种方法(如果由于某些特定原因而需要“ Y”状态)将是在启动时检查“ Y”记录,例如,使用@PostConstruct注释的方法

As you tagged you question with spring , you should simply use a singleton bean with a destroy-method method : it will be destroyed when the application context is closed and that happens when the application itself is stopped. 当您用spring标记问题时,您应该仅使用具有destroy-method方法的单例bean:当应用程序上下文关闭时,它将被销毁;而当应用程序本身停止时,它将被销毁。

XML : <bean class="..." destroy-method="destroy"/> XML: <bean class="..." destroy-method="destroy"/>

JavaConfig : JavaConfig:

@Bean(destroy-method = "destroy")
public class ... {
    public void destroy() {
        // do you cleanup
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM