User Tools

Site Tools


org.gramar.storm.gramar:walkthrough

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision Both sides next revision
org.gramar.storm.gramar:walkthrough [2016/08/12 14:29]
chrisgerken
org.gramar.storm.gramar:walkthrough [2016/08/12 17:58]
chrisgerken
Line 235: Line 235:
   * Each spout helper class has a nextTuples() method that takes a reference to the spout'​s interface. ​ Use this interface to emit tuples, either with a message ID for reliable topologies or without a message ID for unreliable topologies.   * Each spout helper class has a nextTuples() method that takes a reference to the spout'​s interface. ​ Use this interface to emit tuples, either with a message ID for reliable topologies or without a message ID for unreliable topologies.
  
-   +==== Adding Your Business Logic ====
  
 +Let's take a look at how you'd modify a spout helper class to add your business logic.
  
 +An important concept with gramars in general and this gramar in particular, is that there are portions of the generated code that you can modify such that if you re-apply the gramar to a modified model those changes you make will be preserved. ​ The general pattern is that pairs of begin/end comments delimit the regions whose contents are kept on a gramar re-apply. ​ For example, the declaration section below falls within a begin/end comment block, indicating that if we had declarations for this class that those declarations will be kept when we re-apply the gramar. And we will.  Many times.
 +
 +<​code>​
 + // Begin declarations
 +  
 + private static final long serialVersionUID = 1L;
 +
 +        private static final Logger log = Logger.getLogger(APIReaderSpoutLogic.class);​
 +        private boolean written = false;
 +
 +          // This is added, but not set.  We'll assign this variable a value in the open() method
 +          ​
 +        private MyMiddleWareClient client;
 +        ​
 + // End declarations ​
 +
 +</​code> ​
 +
 +The open() method is passed the Config, TopologyContext and a reference to the actual spout. ​ As generated, the Topology driver class (in the **.topology** package) reads the entire property file and moves all of the key/value pairs there into the Config. ​ Use these properties to construct any variables you'll need for the running of the spout. ​ Note that there'​s a close() method, too, if you have shut-down logic. Remember to keep all of your code between the begin/end comment pairs.
 +
 +<​code>​
 +
 +    public void open(Map map, TopologyContext topologyContext,​ IAPIReaderSpout spout) {
 +
 + // Begin open() logic 
 + 
 + // End open() logic 
 +
 +    }
 +
 +    public void close(IAPIReaderSpout spout) {
 +
 + // Begin close() logic 
 +
 +
 + // End close() logic 
 +
 +    }
 +
 +</​code> ​
 +
 +The nextTuple() method is where you put your code business logic. ​ In the example below, we've added three lines of code to construct an instance of RawMessage (the type which defines the shape of stream rawMessages) and then to emit that bean onto the rawMessages stream. ​ Generated logic in the spout class with marshal the fields in the RawMessage object into a Values object and then emit that values object to the stream.
 +
 +Note the generated emit() message for the stream. ​ There will be an explicitle names emit() message for each stream that the spout is defined to have.
 +
 +Note also the other spout lifecycle methods below.
 +
 +<​code>​
 +
 +    public void nextTuple(final IAPIReaderSpout spout) {
 +
 + // Begin nextTuple() logic 
 +
 +        try {
 +
 + // emit a tuple
 +
 +            String payload = "​...."; ​ // Get this value from the middleware
 +            RawMessage rawMessage = new RawMessage(payload);​
 +            spout.emitToRawMessages(rawMessage,​ rawMessage);​
 +
 +       
 +        } catch (Exception e) {
 +       ​ log.error("​APIReaderSpoutLogic nextTuple() error: "+ e.toString());​
 +        }
 +
 + // End nextTuple() logic 
 +
 +    }
 +
 +    public void open(Map map, TopologyContext topologyContext,​ IAPIReaderSpout spout) {
 +
 + // Begin open() logic 
 + 
 + // End open() logic 
 +
 +    }
 +
 +    public void close(IAPIReaderSpout spout) {
 +
 + // Begin close() logic 
 +
 +
 + // End close() logic 
 +
 +    }
 +
 +    public void activate(IAPIReaderSpout spout) {
 +
 + // Begin activate() logic 
 +
 +
 + // End activate() logic 
 +
 +    }
 +
 +    public void deactivate(IAPIReaderSpout spout) {
 +
 + // Begin deactivate() logic 
 +
 +
 + // End deactivate() logic 
 +
 +    }
 +
 +    public void ack(Object o, IAPIReaderSpout spout) {
 +
 + // Begin ack() logic 
 +
 +
 + // End ack() logic 
 +
 +    }
 +
 +</​code>​
org.gramar.storm.gramar/walkthrough.txt ยท Last modified: 2016/08/13 11:41 by chrisgerken