1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.ksb.messaging.exceptionhandling; |
17 | |
|
18 | |
import javax.xml.namespace.QName; |
19 | |
|
20 | |
import org.apache.commons.lang.StringUtils; |
21 | |
import org.apache.log4j.Logger; |
22 | |
import org.kuali.rice.core.api.exception.RiceRuntimeException; |
23 | |
import org.kuali.rice.core.api.reflect.ObjectDefinition; |
24 | |
import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader; |
25 | |
import org.kuali.rice.ksb.api.KsbApiServiceLocator; |
26 | |
import org.kuali.rice.ksb.api.bus.Endpoint; |
27 | |
import org.kuali.rice.ksb.api.bus.ServiceBus; |
28 | |
import org.kuali.rice.ksb.api.messaging.AsynchronousCall; |
29 | |
import org.kuali.rice.ksb.messaging.PersistedMessageBO; |
30 | |
import org.kuali.rice.ksb.messaging.quartz.MessageServiceExecutorJob; |
31 | |
import org.kuali.rice.ksb.messaging.quartz.MessageServiceExecutorJobListener; |
32 | |
import org.kuali.rice.ksb.service.KSBServiceLocator; |
33 | |
import org.quartz.JobDataMap; |
34 | |
import org.quartz.JobDetail; |
35 | |
import org.quartz.Scheduler; |
36 | |
import org.quartz.SimpleTrigger; |
37 | |
import org.quartz.Trigger; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | 0 | public class DefaultExceptionServiceImpl implements ExceptionRoutingService { |
49 | |
|
50 | 0 | private static final Logger LOG = Logger.getLogger(DefaultExceptionServiceImpl.class); |
51 | |
|
52 | |
public void placeInExceptionRouting(Throwable throwable, PersistedMessageBO message, Object service) throws Exception { |
53 | 0 | LOG.error("Exception caught processing message " + message.getRouteQueueId() + " " + message.getServiceName() + ": " + throwable); |
54 | |
|
55 | |
|
56 | 0 | AsynchronousCall methodCall = null; |
57 | 0 | if (message.getMethodCall() != null) { |
58 | 0 | methodCall = message.getMethodCall(); |
59 | |
} else { |
60 | 0 | methodCall = message.getPayload().getMethodCall(); |
61 | |
} |
62 | 0 | message.setMethodCall(methodCall); |
63 | 0 | MessageExceptionHandler exceptionHandler = getMessageExceptionHandler(methodCall.getServiceConfiguration().getServiceName()); |
64 | 0 | exceptionHandler.handleException(throwable, message, service); |
65 | 0 | } |
66 | |
|
67 | |
public void placeInExceptionRoutingLastDitchEffort(Throwable throwable, PersistedMessageBO message, Object service) throws Exception { |
68 | 0 | LOG.error("Exception caught processing message " + message.getRouteQueueId() + " " + message.getServiceName() + ": " + throwable); |
69 | |
|
70 | 0 | AsynchronousCall methodCall = null; |
71 | 0 | if (message.getMethodCall() != null) { |
72 | 0 | methodCall = message.getMethodCall(); |
73 | |
} else { |
74 | 0 | methodCall = message.getPayload().getMethodCall(); |
75 | |
} |
76 | 0 | message.setMethodCall(methodCall); |
77 | 0 | MessageExceptionHandler exceptionHandler = getMessageExceptionHandler(methodCall.getServiceConfiguration().getServiceName()); |
78 | 0 | exceptionHandler.handleExceptionLastDitchEffort(throwable, message, service); |
79 | 0 | } |
80 | |
|
81 | |
protected MessageExceptionHandler getMessageExceptionHandler(QName serviceName) { |
82 | 0 | ServiceBus serviceBus = KsbApiServiceLocator.getServiceBus(); |
83 | 0 | Endpoint endpoint = serviceBus.getEndpoint(serviceName); |
84 | 0 | if (endpoint == null) { |
85 | 0 | throw new RiceRuntimeException("No services found for name " + serviceName); |
86 | |
} |
87 | 0 | String messageExceptionHandlerName = endpoint.getServiceConfiguration().getMessageExceptionHandler(); |
88 | 0 | if (messageExceptionHandlerName == null) { |
89 | 0 | messageExceptionHandlerName = DefaultMessageExceptionHandler.class.getName(); |
90 | |
} |
91 | 0 | return (MessageExceptionHandler) GlobalResourceLoader.getObject(new ObjectDefinition(messageExceptionHandlerName)); |
92 | |
} |
93 | |
|
94 | |
|
95 | |
|
96 | |
public void scheduleExecution(Throwable throwable, PersistedMessageBO message, String description) throws Exception { |
97 | 0 | KSBServiceLocator.getMessageQueueService().delete(message); |
98 | 0 | Scheduler scheduler = KSBServiceLocator.getScheduler(); |
99 | 0 | JobDataMap jobData = new JobDataMap(); |
100 | 0 | jobData.put(MessageServiceExecutorJob.MESSAGE_KEY, message); |
101 | 0 | JobDetail jobDetail = new JobDetail("Exception_Message_Job " + Math.random(), "Exception Messaging", |
102 | |
MessageServiceExecutorJob.class); |
103 | 0 | jobDetail.setJobDataMap(jobData); |
104 | 0 | if (!StringUtils.isBlank(description)) { |
105 | 0 | jobDetail.setDescription(description); |
106 | |
} |
107 | 0 | jobDetail.addJobListener(MessageServiceExecutorJobListener.NAME); |
108 | 0 | Trigger trigger = new SimpleTrigger("Exception_Message_Trigger " + Math.random(), "Exception Messaging", message |
109 | |
.getQueueDate()); |
110 | 0 | trigger.setJobDataMap(jobData); |
111 | 0 | scheduler.scheduleJob(jobDetail, trigger); |
112 | 0 | } |
113 | |
|
114 | |
} |