Your SlideShare is downloading. ×
0
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Code Samples
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Code Samples

7,197

Published on

API examples

API examples

Published in: Technology, News & Politics
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
7,197
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
17
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Jactor Code Samples● Slides: http://sourceforge.net/projects/jactor/files/Code%20Samples%2012-JUN-2012.pdf/download● Code: https://github.com/anirbanbhattacharjee/jactor-in-action
  • 2. JActor● Getting Started● Basic Code Samples● State Machines
  • 3. Getting Started● The JActor jar file● Create a test environment
  • 4. The JActor jar file● Download Jactor-3.1.0.zip from https://sourceforge.net/projects/jactor/files/● Extract jactor-3.1.0.jar from the zip file and copy it to a new directory, GettingStarted.● (The JActor version will change—current version is 3.1.0.)
  • 5. Create a Test Environment● Create a j.bat file in the GettingStarted directory: del *.class call javac -classpath jactor-3.1.0.jar *.java call java -classpath .;jactor-3.1.0.jar GettingStarted● The j.bat file can be used to run a test.
  • 6. Basic Code Samples● Hello world!● Actor <==> Actor● RP Games● Parallel Programming● Dependency Injection● Exception Handling● Loops
  • 7. Hello world!● GettingStarted.java (main)● Test.java (actor)● Start.java (request)
  • 8. GettingStarted.java (main)import org.agilewiki.jactor.*;public class GettingStarted { public static void main(String[] args) throws Exception { MailboxFactory mailboxFactory = JAMailboxFactory.newMailboxFactory(10); Mailbox mailbox = mailboxFactory.createMailbox(); JAFuture future = new JAFuture(); Test test = new Test(); test.initialize(mailbox); Start.req.send(future, test); mailboxFactory.close(); }}
  • 9. Test.java (actor)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Test extends JLPCActor { public void start(RP rp) throws Exception { System.out.println("Hello world!"); rp.processResponse(null); }}
  • 10. Start.java (request)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Start extends Request<Object, Test> { public static final Start req = new Start(); public void processRequest(JLPCActor targetActor, RP rp) throws Exception { Test a = (Test) targetActor; a.start(rp); } public boolean isTargetType(Actor targetActor) { return targetActor instanceof Test; }}
  • 11. OutputHello world!GettingStarted>
  • 12. Actor <==> Actor● Test.java (actor)● Greeter.java (actor)● Greet.java (request)
  • 13. Test.java (actor)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Test extends JLPCActor { public void start(final RP rp) throws Exception { Greeter greeter = new Greeter(); greeter.initialize(getMailbox()); Greet.req.send(this, greeter, new RP<String>() { public void processResponse(String greeting) throws Exception { System.out.println(greeting); rp.processResponse(null); } }); }}
  • 14. Greeter.java (actor)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Greeter extends JLPCActor { public void greet(RP rp) throws Exception { rp.processResponse("Hello world!"); }}
  • 15. Greet.java (request)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Greet extends Request<String, Greeter> { public static final Greet req = new Greet(); public void processRequest(JLPCActor targetActor, RP rp) throws Exception { Greeter a = (Greeter) targetActor; a.greet(rp); } public boolean isTargetType(Actor targetActor) { return targetActor instanceof Greeter; }}
  • 16. OutputHello world!GettingStarted>
  • 17. RP Games● Test.java (actor)● Greeter.java (actor)● Trigger.java (request)
  • 18. Test.java (actor)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Test extends JLPCActor { public void start(final RP rp) throws Exception { Greeter greeter = new Greeter(); greeter.initialize(getMailbox()); Greet.req.send(this, greeter, new RP<String>() { public void processResponse(String greeting) throws Exception { System.out.println(greeting); rp.processResponse(null); } }); System.out.println("trigger..."); Trigger.req.sendEvent(this, greeter); }}
  • 19. Greeter.java (actor)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Greeter extends JLPCActor { private RP rp; public void greet(RP rp) throws Exception { this.rp = rp; } public void trigger(RP rp) throws Exception { rp.processResponse(null); this.rp.processResponse("Hello world!"); }}
  • 20. Trigger.java (request)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Trigger extends Request<Object, Greeter> { public static final Trigger req = new Trigger(); public void processRequest(JLPCActor targetActor, RP rp) throws Exception { Greeter a = (Greeter) targetActor; a.trigger(rp); } public boolean isTargetType(Actor targetActor) { return targetActor instanceof Greeter; }}
  • 21. OutputTrigger...Hello world!GettingStarted>
  • 22. Parallel Programming● Test.java (actor)● Timer.java (actor)● Delay.java (request)
  • 23. Test.java (actor)public class Test extends JLPCActor { public void start(final RP rp) throws Exception { Timer timer1 = new Timer(); timer1.initialize( getMailboxFactory().createAsynchronousMailbox()); Timer timer2 = new Timer(); timer2.initialize( getMailboxFactory().createAsynchronousMailbox()); final long t0 = System.currentTimeMillis(); RP prp = new RP() { boolean pending = true; public void processResponse(Object obj) throws Exception { if (pending) pending = false; else { System.out.println(System.currentTimeMillis()-t0); rp.processResponse(null); } } }; (new Delay(1000)).send(this, timer1, prp); (new Delay(1000)).send(this, timer2, prp); }}
  • 24. Timer.java (actor)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Timer extends JLPCActor { public void delay(int ms, RP rp) throws Exception { Thread.sleep(ms); rp.processResponse(null); }}
  • 25. Delay.java (request)public class Delay extends Request<Object, Timer> { public final int ms; public Delay(int ms) { this.ms = ms; } public void processRequest(JLPCActor targetActor, RP rp) throws Exception { Timer a = (Timer) targetActor; a.delay(ms, rp); } public boolean isTargetType(Actor targetActor) { return targetActor instanceof Timer; }}
  • 26. Output1003GettingStarted>
  • 27. Dependency Injection● Test.java (actor)● Greeter.java (actor)● Printer.java(actor)● Print.java (request)
  • 28. Test.java (actor)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Test extends JLPCActor { public void start(final RP rp) throws Exception { Printer printer = new Printer(); printer.initialize(getMailbox()); final Greeter greeter = new Greeter(); greeter.initialize(getMailbox(), printer); (new Print("Greeting:")). send(this, greeter, new RP() { public void processResponse(Object rsp) throws Exception { Greet.req.send(Test.this, greeter, rp); } }); }}
  • 29. Greeter.java (actor)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Greeter extends JLPCActor { public void greet(RP rp) throws Exception { (new Print("Hello world!")).send(this, this, rp); }}
  • 30. Printer.java (actor)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Printer extends JLPCActor { public void print(String value, RP rp) throws Exception { System.out.println(value); rp.processResponse(null); }}
  • 31. Print.java (request)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Print extends Request<Object, Printer> { public final String value; public Print(String value) { this.value = value; } public void processRequest(JLPCActor targetActor, RP rp) throws Exception { Printer a = (Printer) targetActor; a.processRequest(value, rp); } public boolean isTargetType(Actor targetActor) { return targetActor instanceof Printer; }}
  • 32. OutputGreeting:Hello world!GettingStarted>
  • 33. Exception Handling● Test.java (actor)● ThrowsException.java (actor)● ThrowException.java (request)
  • 34. Test.java (actor)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Test extends JLPCActor { public void start(final RP rp) throws Exception { ThrowsException throwsException = new ThrowsException(); throwsException.initialize(getMailbox()); setExceptionHandler(new ExceptionHandler() { public void process(Exception exception) throws Exception { System.out.println(exception.getMessage()); rp.processResponse(null); } }); ThrowException.req.send(this, throwsException, new RP() { public void processResponse(Object response) throws Exception { System.out.println("No exception"); rp.processResponse(null); } }); }}
  • 35. ThrowsException (actor)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class ThrowsException extends JLPCActor { public void throwException(RP rp) throws Exception { throw new Exception("Boo!"); }}
  • 36. ThrowException (request)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class ThrowException extends Request<Object, ThrowsException> { public static final ThrowException req = new ThrowException(); public void processRequest(JLPCActor targetActor, RP rp) throws Exception { ThrowsException a = (ThrowsException) targetActor; a.throwException(rp); } public boolean isTargetType(Actor targetActor) { return targetActor instanceof ThrowsException; }}
  • 37. OutputBoo!GettingStarted>
  • 38. Loops● Test.java (actor)
  • 39. Test.java (actor)public class Test extends JLPCActor { public void start(RP rp) throws Exception { Printer printer = new Printer(); printer.initialize(getMailbox()); final int max = 5; (new JAIterator() { int i = 0; protected void process(RP irp) throws Exception { if (i == max) irp.processResponse("done"); else { i += 1; (new Print(""+i)). send(Test.this, printer, irp); } } }).iterate(rp); }}
  • 40. Output12345GettingStarted>
  • 41. State Machines● A Sequence of Requests● Looping
  • 42. A Sequence of Requests● Test.java (actor)
  • 43. Test.java (actor)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;public class Test extends JLPCActor { public void start(RP rp) throws Exception { Printer printer = new Printer(); printer.initialize(getMailbox()); SMBuilder smb = new SMBuilder(); smb._send(printer, new Print("a")); smb._send(printer, new Print("b")); smb._send(printer, new Print("c")); smb.call(rp); }}
  • 44. OutputabcGettingStarted>
  • 45. Looping● Test.java (actor)
  • 46. Test.java (actor)import org.agilewiki.jactor.*;import org.agilewiki.jactor.lpc.*;import org.agilewiki.jactor.stateMachine.*;public class Test extends JLPCActor { int counter; public void start(RP rp) throws Exception { Printer printer = new Printer(); printer.initialize(getMailbox()); SMBuilder smb = new SMBuilder(); counter = 1; smb._label("loop"); smb._send(printer, new ObjectFunc() { public Object get(StateMachine sm) { return new Print(""+counter); } }); smb._if(new BooleanFunc() { public boolean get(StateMachine sm) { counter += 1; return counter < 6; } }, "loop"); smb.call(rp); }}
  • 47. Output12345GettingStarted>

×