17.01.2015 Views

Erlang and OTP in Action.pdf - Synrc

Erlang and OTP in Action.pdf - Synrc

Erlang and OTP in Action.pdf - Synrc

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

339<br />

name off the comm<strong>and</strong> l<strong>in</strong>e start<strong>in</strong>g at annotation #1. It also <strong>in</strong>stantiates the<br />

SimpleCacheHBaseMa<strong>in</strong> class <strong>and</strong> calls the process method. The process method itself is<br />

where the magic lies <strong>and</strong> it is illustrated <strong>in</strong> code list<strong>in</strong>g 13.4.<br />

13.4 The Process Method<br />

while (true) {<br />

try {<br />

Otp<strong>Erlang</strong>Object o = _mbox.receive();<br />

assert (o <strong>in</strong>stanceof Otp<strong>Erlang</strong>Tuple); #1<br />

Otp<strong>Erlang</strong>Tuple msg = (Otp<strong>Erlang</strong>Tuple) o; #2<br />

Otp<strong>Erlang</strong>Pid from = (Otp<strong>Erlang</strong>Pid) msg.elementAt(0);<br />

Otp<strong>Erlang</strong>Ref ref (Otp<strong>Erlang</strong>Ref) msg.elementAt(1);<br />

Str<strong>in</strong>g action = ((Otp<strong>Erlang</strong>Atom) msg.elementAt(2)).toStr<strong>in</strong>g();<br />

Str<strong>in</strong>g key = ((Otp<strong>Erlang</strong>List) msg.elementAt(3)).toStr<strong>in</strong>g();<br />

HBaseTask task = null;<br />

if (msg.arity() == 3) { #3<br />

data = ((Otp<strong>Erlang</strong>B<strong>in</strong>ary) msg.elementAt(3)).b<strong>in</strong>aryValue();<br />

task = new HBaseTask(_mbox, _conn, from, action, key, data);<br />

} else if (msg.arity() == 2) { #4<br />

task = new HBaseTask(_mbox, _conn, from, action, key, null);<br />

} else { #5<br />

Otp<strong>Erlang</strong>Tuple res = new Otp<strong>Erlang</strong>Tuple(new Otp<strong>Erlang</strong>Object[] {<br />

new Otp<strong>Erlang</strong>Atom("error"),<br />

new Otp<strong>Erlang</strong>Atom("<strong>in</strong>valid_request") });<br />

}<br />

_mbox.send(from, res);<br />

return;<br />

_exe.submit(task); #6<br />

} catch (Exception e) {<br />

System.out.pr<strong>in</strong>tln("" + e);<br />

}<br />

}<br />

}<br />

We start out <strong>in</strong> the same way as we did <strong>in</strong> our example code for our greet<strong>in</strong>g server by<br />

sett<strong>in</strong>g up an endless loop. This endless loop is just where we sit <strong>in</strong> a loop on the message<br />

receive <strong>and</strong> wait for <strong>in</strong>com<strong>in</strong>g messages the same way as is done <strong>in</strong> <strong>Erlang</strong>/<strong>OTP</strong> servers.<br />

When we get a message <strong>in</strong>to the mailbox we pull it off the queue <strong>and</strong> process it. First we<br />

make sure that the message we receive is a tuple with an assertion at code annotation #1.<br />

For right now, if the <strong>in</strong>com<strong>in</strong>g message is not a tuple we really don’t h<strong>and</strong>le that other than<br />

by pr<strong>in</strong>t<strong>in</strong>g an exception to stdout. Without a properly formed tuple we don’t know who the<br />

sender is <strong>and</strong> so cannot even provide a reply if we wanted to. As this code moves further<br />

forward <strong>in</strong> its read<strong>in</strong>ess for production we will at least want to log out the fact that we got a<br />

©Mann<strong>in</strong>g Publications Co. Please post comments or corrections to the Author Onl<strong>in</strong>e forum:<br />

http://www.mann<strong>in</strong>g-s<strong>and</strong>box.com/forum.jspaforumID=454

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!