Revision 29489
Added by Claudio Atzori almost 10 years ago
modules/cnr-blackboard-common/trunk/src/main/java/eu/dnetlib/enabling/tools/blackboard/AbstractBlackboardNotificationHandler.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.enabling.tools.blackboard; |
2 | 2 |
|
3 | 3 |
import java.io.IOException; |
4 |
import java.util.concurrent.Executor; |
|
5 |
import java.util.concurrent.Executors; |
|
4 | 6 |
|
5 | 7 |
import javax.xml.parsers.ParserConfigurationException; |
6 | 8 |
import javax.xml.xpath.XPathExpressionException; |
... | ... | |
13 | 15 |
|
14 | 16 |
/** |
15 | 17 |
* Common blackboard notification handler. This notification handler processes only message with ...BODY.BLACKBOARD.LAST* as topic. |
16 |
* |
|
17 |
* @param <T> type of blackboard handler used to extract the blackboard message (client or server) |
|
18 |
* |
|
19 |
* @param <T> |
|
20 |
* type of blackboard handler used to extract the blackboard message (client or server) |
|
18 | 21 |
* @author marko |
19 |
* |
|
22 |
*
|
|
20 | 23 |
*/ |
21 | 24 |
public abstract class AbstractBlackboardNotificationHandler<T extends BlackboardHandler> implements NotificationHandler, Enableable { |
22 | 25 |
|
... | ... | |
24 | 27 |
* blackboard handler. |
25 | 28 |
*/ |
26 | 29 |
private T blackboardHandler; |
27 |
|
|
30 |
|
|
28 | 31 |
/** |
29 | 32 |
* true if enabled. |
30 | 33 |
*/ |
31 |
private boolean enabled = true;
|
|
34 |
private boolean enabled = true; |
|
32 | 35 |
|
33 | 36 |
/** |
37 |
* Executor handles the notified request in a dedicated thread and allows to return immediately. |
|
38 |
*/ |
|
39 |
private Executor executor = Executors.newCachedThreadPool(); |
|
40 |
|
|
41 |
/** |
|
34 | 42 |
* {@inheritDoc} |
35 |
* |
|
36 |
* @see eu.dnetlib.data.mdstore.NotificationHandler#notified(java.lang.String, java.lang.String, java.lang.String, |
|
37 |
* java.lang.String) |
|
43 |
* |
|
44 |
* @see eu.dnetlib.data.mdstore.NotificationHandler#notified(java.lang.String, java.lang.String, java.lang.String, java.lang.String) |
|
38 | 45 |
*/ |
39 | 46 |
@Override |
40 | 47 |
public void notified(final String subscrId, final String topic, final String rsId, final String profile) { |
41 |
if (!topic.contains("BODY.BLACKBOARD.LAST")) |
|
42 |
return; |
|
48 |
if (!topic.contains("BODY.BLACKBOARD.LAST")) return; |
|
43 | 49 |
|
44 |
try { |
|
45 |
processJob(blackboardHandler.getJob(new StringOpaqueResource(profile))); |
|
46 |
} catch (final XPathExpressionException e) { |
|
47 |
throw new IllegalStateException(e); |
|
48 |
} catch (final SAXException e) { |
|
49 |
throw new IllegalStateException(e); |
|
50 |
} catch (final IOException e) { |
|
51 |
throw new IllegalStateException(e); |
|
52 |
} catch (final ParserConfigurationException e) { |
|
53 |
throw new IllegalStateException(e); |
|
54 |
} |
|
50 |
executor.execute(new Runnable() { |
|
51 |
|
|
52 |
@Override |
|
53 |
public void run() { |
|
54 |
try { |
|
55 |
processJob(blackboardHandler.getJob(new StringOpaqueResource(profile))); |
|
56 |
} catch (final XPathExpressionException e) { |
|
57 |
throw new IllegalStateException(e); |
|
58 |
} catch (final SAXException e) { |
|
59 |
throw new IllegalStateException(e); |
|
60 |
} catch (final IOException e) { |
|
61 |
throw new IllegalStateException(e); |
|
62 |
} catch (final ParserConfigurationException e) { |
|
63 |
throw new IllegalStateException(e); |
|
64 |
} |
|
65 |
} |
|
66 |
}); |
|
55 | 67 |
} |
56 | 68 |
|
57 | 69 |
/** |
58 | 70 |
* Subclassess override this to process incoming blackboard jobs. |
59 |
* |
|
71 |
*
|
|
60 | 72 |
* @param job |
61 | 73 |
* blackboard job |
62 | 74 |
*/ |
... | ... | |
77 | 89 |
} |
78 | 90 |
|
79 | 91 |
@Override |
80 |
public void setEnabled(boolean enabled) { |
|
92 |
public void setEnabled(final boolean enabled) {
|
|
81 | 93 |
this.enabled = enabled; |
82 | 94 |
} |
83 | 95 |
|
Also available in: Unified diff
delegates the execution to a dedicated thread