Revision 35862
Added by Michele Artini about 9 years ago
BlackboardRegistry.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.enabling.blackboard; |
2 | 2 |
|
3 |
import java.util.Date; |
|
3 | 4 |
import java.util.Map; |
5 |
import java.util.concurrent.Executor; |
|
6 |
import java.util.concurrent.Executors; |
|
4 | 7 |
|
5 | 8 |
import javax.annotation.Resource; |
6 | 9 |
|
... | ... | |
8 | 11 |
import org.apache.commons.logging.LogFactory; |
9 | 12 |
|
10 | 13 |
import com.google.common.collect.Maps; |
14 |
import com.google.gson.Gson; |
|
11 | 15 |
|
12 | 16 |
import eu.dnetlib.common.services.BlackboardAction; |
17 |
import eu.dnetlib.rmi.objects.is.BlackboardActionStatus; |
|
18 |
import eu.dnetlib.rmi.objects.is.BlackboardMessage; |
|
13 | 19 |
import eu.dnetlib.rmi.objects.is.Operation; |
14 | 20 |
import eu.dnetlib.rmi.objects.is.Subscription; |
15 | 21 |
import eu.dnetlib.rmi.soap.InformationService; |
... | ... | |
20 | 26 |
@Resource |
21 | 27 |
private InformationService informationService; |
22 | 28 |
|
29 |
private Executor executor = Executors.newCachedThreadPool(); |
|
30 |
|
|
23 | 31 |
private static final Log log = LogFactory.getLog(BlackboardRegistry.class); |
24 | 32 |
|
25 | 33 |
private Map<String, BlackboardAction<?>> actions = Maps.newHashMap(); |
... | ... | |
34 | 42 |
log.info("BB Subscription " + subscrId + " -> " + action.getClass()); |
35 | 43 |
} |
36 | 44 |
|
37 |
public void activateAction(final String subscrId, final Object message) { |
|
38 |
final BlackboardAction<?> action = actions.get(subscrId); |
|
39 |
if (action == null) { |
|
40 |
throw new RuntimeException("No action has been registered for subscription " + subscrId); |
|
41 |
} else { |
|
42 |
action.process(message); |
|
45 |
public void activateAction(final String subscrId, final BlackboardMessage bbMessage) { |
|
46 |
executor.execute(new Runnable() { |
|
47 |
|
|
48 |
@Override |
|
49 |
public void run() { |
|
50 |
final BlackboardAction<?> action = actions.get(subscrId); |
|
51 |
if (action == null) { |
|
52 |
throw new RuntimeException("No action has been registered for subscription " + subscrId); |
|
53 |
} else { |
|
54 |
try { |
|
55 |
updateBlackboard(BlackboardActionStatus.ONGOING, bbMessage); |
|
56 |
|
|
57 |
final Object newMessage = action.process(bbMessage.getJsonMessage()); |
|
58 |
log.info("RESPONSE: " + newMessage); |
|
59 |
|
|
60 |
bbMessage.setJsonMessage(new Gson().toJson(newMessage)); |
|
61 |
updateBlackboard(BlackboardActionStatus.DONE, bbMessage); |
|
62 |
} catch (Throwable e) { |
|
63 |
bbMessage.setJsonMessage(new Gson().toJson(e)); |
|
64 |
updateBlackboard(BlackboardActionStatus.FAILED, bbMessage); |
|
65 |
} |
|
66 |
} |
|
67 |
} |
|
68 |
}); |
|
69 |
} |
|
70 |
|
|
71 |
private void updateBlackboard(final BlackboardActionStatus status, final BlackboardMessage bbMessage) { |
|
72 |
try { |
|
73 |
bbMessage.setStatus(status); |
|
74 |
bbMessage.setDate(new Date()); |
|
75 |
informationService.updateBlackBoardMessage(bbMessage); |
|
76 |
} catch (InformationServiceException e) { |
|
77 |
log.error("Error updating blackboard message: " + bbMessage); |
|
43 | 78 |
} |
44 | 79 |
} |
80 |
|
|
81 |
public boolean requiresBlackboardAction(final String subscrId) { |
|
82 |
return actions.containsKey(subscrId); |
|
83 |
} |
|
45 | 84 |
} |
Also available in: Unified diff
blackboard status update