Integration of the Framework Akka and Java EE 6
Total Page:16
File Type:pdf, Size:1020Kb
Masaryk University Faculty of Informatics Integration of the framework Akka and Java EE 6 Bachelors thesis Tibor Bogár Brno, autumn 2013 1 Announcement I annouce, that this bachelor's thesis is my original author's work, which I created autonomosly. Every source, strand and literature, which I used or extracted in my work is properly quoted specifying entire reference on the relevant source. Thesis supervisor: doc. RNDr. Tomáš Pitner, Ph.D. 2 Acknowledgement Foremost, I would like to express my sincere gratitude to my advisor Mgr. Martin Švehla, for his patience, motivation and immense knowledge. His guidance helped me in the time of research and writing this thesis. Besides my advisor, I would like to thank my thesis supervisor doc. RNDr. Tomáš Pitner, Ph.D., for his patience and tolerance. My sincere thanks to Helena Kryštofová and whole Office for Studies for helping me with administration. Last and not least, my mother for supporting me and making it possible to work entirely on thesis. 3 Contents 1. Introduction....................................................................................5 2. Context and Dependency Injection.............................................7 2.1. CDI bean characteristic....................................................................8 2.2. Qualifiers............................................................................................8 2.3. Scope...................................................................................................9 2.4. Injection Points..................................................................................9 2.5. Lose coupling with strong typing..................................................10 2.5.1. Producer methods.......................................................................10 2.5.2. Interceptors..................................................................................11 2.5.3. Decorators....................................................................................11 2.5.4. Events...........................................................................................11 2.5.5. Stereotypes...................................................................................12 3. Akka framework...........................................................................13 3.1. Actor Systems...................................................................................13 3.2. Actor Reference................................................................................14 3.3. State …...............................................................................................14 3.4. Behavior.............................................................................................15 3.5. Mailbox..............................................................................................15 3.6. Children.............................................................................................16 3.7. Supervisor Strategy..........................................................................16 3.8. When an Actor Terminates.............................................................16 3.9. Creating Actors.................................................................................17 3.9.1. Untyped Actor.............................................................................17 3.9.2. Props.............................................................................................17 3.9.3. Typed Actor.................................................................................18 4. Application.....................................................................................19 5. CDI Extension................................................................................21 6. Akka Modul.....…..........................................................................25 7. Examples in Spring and Google Guice......................................27 8. Conclusion......................................................................................29 9. Reference........................................................................................30 4 1. Introduction The goal of this work was to make an extension for Context and Depedency Injection [1] and a modul for framework Akka [2], which allows to use design pattern Dependency Injection in Actors of framework Akka i.e. Automatization of creating Java EE components and sources in Akka Actors and creating Actors into Java EE components using anotations. Context and Dependency Injection (CDI) framework deal with inserting dependencies and context in Java EE 6. Main advantage of this framework is to simplify access to objects and unify used services. Accessing to objects is secured throught design pattern inversion of control sometimes also called depednency injection, where we access to objects not directly through creating instancies with standard constructing methods, but our application server is managing the whole lifecycle of a corectly created class. These objects are called beans. CDI includes built-in support for several different kinds of bean, including Java EE component types as managed beans and EJB session beans. Prior to Java EE 6, there was no clear definition of the term "bean" in the Java EE platform. Java EE 6 finally lays down that common definition in the Managed Beans specification. Managed Beans are defined as container-managed objects with minimal programming restrictions, otherwise known by the acronym POJO (Plain Old Java Object). CDI allows the container to create and destroy instances of your beans and associate them with a designated context, injecting them into other beans, using them in EL expressions, specializing them with qualifier annotations, even adding interceptors and decorators to them. Akka framework uses Actor Model to raise the abstraction level and provide a better platform to build concurrent scalable and fault-tolerant applications. Any system that have the need for high- throughput and low latency can be easily implemented using Akka framework. Thnks to Actor Model developers musnt take care of managing threads and accesing to mutable data objects. Actors are objects which encapsulate state and behavior, they communicate exclusively by exchanging messages. In a sense, actors are the most stringent form of object-oriented programming, but it serves better to view them as persons: while modeling a solution with actors, imagine a group of people and assign sub-tasks to them, arrange 5 their functions into an organizational structure and think about how to deal with a failure and not careing for their emotional state or moral issues. Actors form hierarchies in terms of one actor with a certain function can split up tasks into smaller more managable pieces. Every Actor which splits his task into other actors must supervise these newly created actors. It is called parental supervision. This spliting tasks into smaller and smaller ones helps us to solve complex problems and makeing them easier to work up. There may be milions of actors within one system. The exact order in which messages are processed in large systems is not controllable by the application author, so we let Akka to take care of that. An actor object needs to be shielded from the outside in order to benefit from the actor model. Therefore, actors are represented to the outside using actor references, which are objects that can be passed around freely and without restriction. This split into inner and outer object enables transparency for all the desired operations and making impossible to look inside an actor and see its state from the outside, unless an actor publishes this information itself. Actors communicate with oneanother by exchanging messages. An actor can recieve message from other actors or from outside the actor system and its purpouse is to process these messages. Each actor has exactly one mailbox to which all senders enqueue their messages. Messages in mailboxes are defualtly defined by FIFO queue's algorithm, but they can be also prioritized. Actors always handle the next dequeued message. First of all we had to read and understand the uppermentioned frameworks and learn how to use them. Integrating modules are now used in many important and often used frameworks e.g. Spring and Google Guice. There is often a loss of clarity in writing new applications and therefore every developer wants the process of writing source code to be simple and a lot of things to be delegated and automatized. This simplifing of creating source codes is called convention over configuration. By decreasing the amount of decisions there is often a loss in flexibility. Our integration solution bring us simplicity but it doesnt lose much flexibility, because for example in case of creating new ActorSystem we can still use the old method of instanciating. Work on the implementation consists of two tasks. First of all we had to create a simple application based on assigment of consultant Mr. Mgr. Martin Švehla, on which we can see the function of our CDI extension and Akka module. The first task was to make extension for CDI framework, 6 where we had worked on automatic creating of actor classes, which were being created by the application server. We had to decide how to mark and specify the actor, which we wanted to instanciate by application server. We decide to mark the actors by annotation Named. This was easier as anotating it by annotation Qualifiers. Actors marked by annotation Named must have unique name so as application server could specify