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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

328<br />

perform the same function. However, they are not processes themselves. JInterface allows<br />

you, the Java programmer, to manage threads <strong>in</strong> any way that you would like. Therefore<br />

gives you direct access to this mailbox abstraction so that your threads can <strong>in</strong>teract with<br />

nodes as they will.<br />

We create the mailbox by ask<strong>in</strong>g an OtpNode object to create a mailbox for us. We have<br />

two k<strong>in</strong>ds of mailboxes that can be created, one with a name <strong>and</strong> one without. If we create a<br />

mailbox with a name that name is registered <strong>in</strong> the exact same way a registered process is.<br />

That is we can send messages to the mailbox us<strong>in</strong>g a registered name just like any other<br />

registered process. If we create the mbox without a name then it acts like any other<br />

anonymous process <strong>and</strong> we need the PID to <strong>in</strong>teract with it. Yes, JInterface mailboxes also<br />

have a PID like any other <strong>Erlang</strong> process. Let’s look at how these are actually created <strong>in</strong> the<br />

next snippet.<br />

OtpMbox mbox = node.createMbox(“our_registered_name”);<br />

To create an mbox without a registered name we just do the same without actually pass<strong>in</strong>g<br />

<strong>in</strong> the name to our node.<br />

OtpMbox mbox = node.createMbox();<br />

Once we have the mailbox we can use it to send <strong>and</strong> receive messages. The two<br />

fundamental APIs we are go<strong>in</strong>g to use to do this are the send <strong>and</strong> receive methods of the<br />

OtpMbox class. There are several variations of these methods, but I will leave it to you to<br />

take a look at the javadocs for the variations. Send<strong>in</strong>g <strong>and</strong> receiv<strong>in</strong>g data requires that we<br />

marshal the data to <strong>and</strong> from the native <strong>Erlang</strong> format. Fortunately, for us JInterface gives<br />

us the tools we need to be able to do that.<br />

Mapp<strong>in</strong>g <strong>Erlang</strong> Data structures Onto Java<br />

Anyth<strong>in</strong>g we send to or receive from an <strong>Erlang</strong> node or anyth<strong>in</strong>g masquerad<strong>in</strong>g as an<br />

<strong>Erlang</strong> node needs to be marshaled to <strong>and</strong> from the native <strong>Erlang</strong> types. We do this by us<strong>in</strong>g<br />

the type mapp<strong>in</strong>g classes that JInterface provides for us. These classes are a direct<br />

representation of the <strong>Erlang</strong> types <strong>in</strong> Java.<br />

Table 13.1 <strong>Erlang</strong> Java Mapp<strong>in</strong>gs<br />

<strong>Erlang</strong> Type<br />

Atom<br />

Java Class<br />

Otp<strong>Erlang</strong>Atom<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!