के लिए अपवादों को संभालना मेरे पास निम्न कोड स्निपेट है जो मूल रूप से कार्य की सूची के माध्यम से स्कैन करता है जिसे निष्पादित करने की आवश्यकता होती है और प्रत्येक कार्य निष्पादक को निष्पादन के लिए दिया जाता है।ThreadPoolExecutor
JobExecutor
बदले में एक और निष्पादक (डीबी सामान करने के लिए ... कतार में डेटा पढ़ने और लिखने के लिए) बनाता है और कार्य को पूरा करता है।
JobExecutor
सबमिट किए गए कार्यों के लिए Future<Boolean>
देता है। जब कोई कार्य विफल रहता है, तो मैं सभी धागे को गहराई से बाधित करना चाहता हूं और सभी अपवादों को पकड़कर निष्पादक को बंद करना चाहता हूं। मुझे क्या परिवर्तन करने की ज़रूरत है?
public class DataMovingClass {
private static final AtomicInteger uniqueId = new AtomicInteger(0);
private static final ThreadLocal<Integer> uniqueNumber = new IDGenerator();
ThreadPoolExecutor threadPoolExecutor = null ;
private List<Source> sources = new ArrayList<Source>();
private static class IDGenerator extends ThreadLocal<Integer> {
@Override
public Integer get() {
return uniqueId.incrementAndGet();
}
}
public void init(){
// load sources list
}
public boolean execute() {
boolean succcess = true ;
threadPoolExecutor = new ThreadPoolExecutor(10,10,
10, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(1024),
new ThreadFactory() {
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setName("DataMigration-" + uniqueNumber.get());
return t;
}// End method
}, new ThreadPoolExecutor.CallerRunsPolicy());
List<Future<Boolean>> result = new ArrayList<Future<Boolean>>();
for (Source source : sources) {
result.add(threadPoolExecutor.submit(new JobExecutor(source)));
}
for (Future<Boolean> jobDone : result) {
try {
if (!jobDone.get(100000, TimeUnit.SECONDS) && success) {
// in case of successful DbWriterClass, we don't need to change
// it.
success = false;
}
} catch (Exception ex) {
// handle exceptions
}
}
}
public class JobExecutor implements Callable<Boolean> {
private ThreadPoolExecutor threadPoolExecutor ;
Source jobSource ;
public SourceJobExecutor(Source source) {
this.jobSource = source;
threadPoolExecutor = new ThreadPoolExecutor(10,10,10, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(1024),
new ThreadFactory() {
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setName("Job Executor-" + uniqueNumber.get());
return t;
}// End method
}, new ThreadPoolExecutor.CallerRunsPolicy());
}
public Boolean call() throws Exception {
boolean status = true ;
System.out.println("Starting Job = " + jobSource.getName());
try {
// do the specified task ;
}catch (InterruptedException intrEx) {
logger.warn("InterruptedException", intrEx);
status = false ;
} catch(Exception e) {
logger.fatal("Exception occurred while executing task "+jobSource.getName(),e);
status = false ;
}
System.out.println("Ending Job = " + jobSource.getName());
return status ;
}
}
}
ठीक है .. क्या आपको कोई अन्य त्रुटियां या स्थान दिखाई देता है जहां मुझे अपवादों को संभालने की आवश्यकता होती है? – jagamot