|
2 | 2 |
|
3 | 3 | import backtype.storm.generated.ShellComponent;
|
4 | 4 | import backtype.storm.task.TopologyContext;
|
| 5 | +import backtype.storm.utils.ShellProcess; |
| 6 | +import backtype.storm.utils.Utils; |
5 | 7 | import java.util.Map;
|
| 8 | +import java.util.List; |
| 9 | +import java.io.IOException; |
| 10 | +import org.apache.log4j.Logger; |
| 11 | +import org.json.simple.JSONObject; |
6 | 12 |
|
| 13 | +import static backtype.storm.Config.TOPOLOGY_MAX_SPOUT_PENDING; |
7 | 14 |
|
8 | 15 | public class ShellSpout implements ISpout {
|
| 16 | + public static Logger LOG = Logger.getLogger(ShellSpout.class); |
| 17 | + |
| 18 | + private SpoutOutputCollector _collector; |
| 19 | + private String[] _command; |
| 20 | + private ShellProcess _process; |
| 21 | + |
9 | 22 | public ShellSpout(ShellComponent component) {
|
10 | 23 | this(component.get_execution_command(), component.get_script());
|
11 | 24 | }
|
12 | 25 |
|
13 |
| - public ShellSpout(String shellCommand, String codeResource) { |
14 |
| - |
| 26 | + public ShellSpout(String... command) { |
| 27 | + _command = command; |
15 | 28 | }
|
16 | 29 |
|
17 |
| - public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) { |
18 |
| - throw new UnsupportedOperationException("Not supported yet."); |
| 30 | + public void open(Map stormConf, TopologyContext context, |
| 31 | + SpoutOutputCollector collector) { |
| 32 | + _process = new ShellProcess(_command); |
| 33 | + _collector = collector; |
| 34 | + |
| 35 | + try { |
| 36 | + String subpid = _process.launch(stormConf, context); |
| 37 | + LOG.info("Launched subprocess with pid " + subpid); |
| 38 | + } catch (IOException e) { |
| 39 | + throw new RuntimeException("Error when launching multilang subprocess", e); |
| 40 | + } |
19 | 41 | }
|
20 | 42 |
|
21 | 43 | public void close() {
|
22 |
| - throw new UnsupportedOperationException("Not supported yet."); |
| 44 | + _process.destroy(); |
23 | 45 | }
|
24 | 46 |
|
| 47 | + private JSONObject _next; |
25 | 48 | public void nextTuple() {
|
26 |
| - throw new UnsupportedOperationException("Not supported yet."); |
| 49 | + if (_next == null) { |
| 50 | + _next = new JSONObject(); |
| 51 | + _next.put("command", "next"); |
| 52 | + } |
| 53 | + |
| 54 | + querySubprocess(_next); |
27 | 55 | }
|
28 | 56 |
|
| 57 | + private JSONObject _ack; |
29 | 58 | public void ack(Object msgId) {
|
30 |
| - throw new UnsupportedOperationException("Not supported yet."); |
| 59 | + if (_ack == null) { |
| 60 | + _ack = new JSONObject(); |
| 61 | + _ack.put("command", "ack"); |
| 62 | + } |
| 63 | + |
| 64 | + _ack.put("id", msgId); |
| 65 | + querySubprocess(_ack); |
31 | 66 | }
|
32 | 67 |
|
| 68 | + private JSONObject _fail; |
33 | 69 | public void fail(Object msgId) {
|
34 |
| - throw new UnsupportedOperationException("Not supported yet."); |
| 70 | + if (_fail == null) { |
| 71 | + _fail = new JSONObject(); |
| 72 | + _fail.put("command", "fail"); |
| 73 | + } |
| 74 | + |
| 75 | + _fail.put("id", msgId); |
| 76 | + querySubprocess(_fail); |
35 | 77 | }
|
36 | 78 |
|
| 79 | + private void querySubprocess(Object query) { |
| 80 | + try { |
| 81 | + _process.writeObject(query); |
| 82 | + |
| 83 | + while (true) { |
| 84 | + Map action = _process.readMap(); |
| 85 | + if (action == null) return; // sync |
| 86 | + String command = (String) action.get("command"); |
| 87 | + if (command.equals("log")) { |
| 88 | + String msg = (String) action.get("msg"); |
| 89 | + LOG.info("Shell msg: " + msg); |
| 90 | + } else if (command.equals("emit")) { |
| 91 | + String stream = (String) action.get("stream"); |
| 92 | + if (stream == null) stream = Utils.DEFAULT_STREAM_ID; |
| 93 | + Long task = (Long) action.get("task"); |
| 94 | + List<Object> tuple = (List) action.get("tuple"); |
| 95 | + Object messageId = (Object) action.get("id"); |
| 96 | + if (task == null) { |
| 97 | + List<Integer> outtasks = _collector.emit(stream, tuple, messageId); |
| 98 | + _process.writeObject(outtasks); |
| 99 | + } else { |
| 100 | + _collector.emitDirect((int)task.longValue(), stream, tuple, messageId); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } catch (IOException e) { |
| 105 | + throw new RuntimeException(e); |
| 106 | + } |
| 107 | + } |
37 | 108 | }
|
0 commit comments