• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Erlang vs. Java
 

Erlang vs. Java

on

  • 8,315 views

 

Statistics

Views

Total Views
8,315
Views on SlideShare
8,314
Embed Views
1

Actions

Likes
8
Downloads
165
Comments
0

1 Embed 1

http://www.techgig.com 1

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Erlang vs. Java Erlang vs. Java Presentation Transcript

  • Erlang vs. Java January 19, 2011 Artan Cami
  • (i) Erlang history(ii) Erlang resources(iii) Java history(iv) Compare philosophies(v) Problem definition(vi) JEE approach & principles(vii) JEE solution details(viii) Erlang/OTP approach & principles(ix) Erlang solution details(x) Erlang language basics(xi) Speculations
  • erlang history http://www.erlang.org/course/history.html1982-1985 telecom experiments with about 20 languages => need high symbolic language1985-1986 experiments with Lisp, Prolog, Parlog... => need concurrency primitives anderror recovery => need new language1987 first experiments with Erlang1988 “Erlang escapes from the lab!”1990 Erlang presented in ISS’90 => new users introduced1992 Erlang ported to VxWorks, PC, Mac ...1993 Distribution is added to Erlang => run homogeneous Erlang to heterogeneoushardware1997 OTP (open telecom platform) started1998 Erlang banned => open source Erlang2002 Concurrency Oriented Programming2006 SMP (symmetric multiprocessing machine) Erlang - Multicore.
  • erlang resources๏ Programming Erlang: Software for a Concurrent World - Joe Armstrong - Jul 2007๏ ERLANG Programming - Francesco Cesarini, Simon Thompson - Jun 2009๏ Erlang and OTP in Action - Martin Logan, Eric Merritt, Richard Carlson - Nov 2010๏ www.erlang.org - Erlang Programming Language, Official Site๏ www.trapexit.org - Erlang Community Site๏ erlyweb.org - The Erlang Twist on Web Frameworks๏ erlide.sourceforge.net - The Erlang IDE. Powered by Eclipse.๏ www.infoq.com/interviews - interviews taken in Erlang Factory events.
  • java history http://www.java.com/en/javahistory1991 The Green Project begins1992 “Oak”1994 WebRunner - first browser for moving objects and executable content1995 Oak becomes Java. Java technology released to a select group on wicked.neato.org1996 JDKtm 1.0 released; first JavaOne conference1997 Over 220,000 downloads of JDK 1.1 in just three weeks; JavaOne worlds largestdeveloper conference with over 8,000 attendees1998 JDK 1.1 2 million downloads; Visa Java Smart Card; JCP formalized1999 J2EE beta released; Java 2 source code released; over 20,000 people in JavaOne2001 Over 2 million downloads of J2EE2003 About 75% of professional developers use Java as their primary language2004 Mars-Rover touches down on Mars; J2SE 5 (Tiger) released2005 4.5 million java developers; 2.5 billion Java-enabled devices
  • philosophies/principles• Java : • “the computer is the network” • write once, run anywhere • simple • concurrency multithreading • object orientated programming• Erlang : • the world is parallel • concurrent processes belong to the language - not OS • very light weight concurrency • share nothing; no mutable state; no locks • pure asynchronous message passing • “let it crash” - no defensive programming • functional programming
  • bookstore problem typical online bookstore- books: find books, display list of books, display the book details • book details: book summary information, editorial reviews, product details, authors details, customer reviews, related books: frequently bought together, other books on this topic, other books by this author(s)- customer: create and maintain profile; store, find, and display transaction history.- orders: shopping cart capability; payment and shipment types; order placement.- non-functional requirements➡ S - store app, B - books apps, C - customer apps; O - order apps
  • jee approach and patterns• MVC• Define layers: presentation, model, and data• Model: EJB3 - stateless, stateful, singleton session beans; message-driven beans; • container management, session facade, entity access object;• ORM layer: JPA implementation • EntityManager, entity POJOs;• Pick a presentation framework: struts, jsf, templating, or portal
  • jee - deployment strategycollocated JVM 1 Applications S B C O JVM 2 Applications Load Balancer S B C O JVM 3 Applications S B C O
  • jee - deployment strategy distributed JVM 2 EJB Applications BEJB JVM 1 Web Applications JVM 3 SWEB BWEB CWEB OWEB EJB Applications CEJB OEJB
  • jee - books applications J V M 1 J V M 2W e b C o n t a i n e r E J B C o n t a i n e r ORM BookEntity BookEAO EJB RelatedBooksEntity BooksController Books EJB BookContentEntity AuthorEntity BooksView RelatedBookslEAO EJB BookDetailView ...View PublisherEntity BookContentView
  • jee -customer applications J V M 1 J V M 3W e b C o n t a i n e r E J B C o n t a i n e r ORM ProfileEntity ProfileEAO EJB CustomerController PaymentTypesEntity Customer EJB OrdersHistoryEntity CustomerProfileView OrdersHistoryEAO ...View EJB OrdersHisotryView
  • jee - orders applications J V M 1 J V M 3W e b C o n t a i n e r E J B C o n t a i n e r ORM ShoppingCartContr ShoppingCartEAO ShoppingCartEntity ShoppingCart EJB EJB OrdersController PaymentEntity PaymentsEAO ShippingEntity EJB ShoppingCartView Orders EJB ...View PaymentView OrdersEntity OrdersEAO EJB
  • jee - sequence diagram display book detailsBooksController BooksEJB BooksEAO RelatedBooksEAO 1 2 3 4 5 7 BookView 6 BookDetailView 8 9 RelatedBooksView 10 11 12 13 14
  • erlang/otp approach and patterns• Use large number of processes in order to distribute/ parallelize as much as possible; • process is the main building block with its own stack, garbage collection, message queue, security model. • message sending/receiving simplicity made possible by pattern matching and simple data structures;• Avoid side effects - no shared resources among processes; • variables that don’t vary - single assignment. No mutable state, no shared memory, no locks;• Small messages, big computations• Distributed database Mnesia• OTP patterns: • gen_server, event_handler, supervisor, application, ...
  • erlang - sequence & concurrency diagram display book details BooksController BooksDetailModel 1 BooksDetailController 2 3 BookDetailView 4 5 6 7 RelatedBooksModel RelatedBooksController 2’ 3’ RelatedBooksView 4’ 5’ 6’ 7’ 8
  • erlang - books applications Node 2 BooksDetails BooksDetails Model Model Node 1 BooksDetails RelatedBooks RelatedBooks Controller Controller View BooksDetail View Books Controller
  • erlang - deployment strategy Distributed Node 2 Node 3 B O C processes processes processes Node 1 OWEB BWEB processes CWEB processes processes SWEB processes
  • erlang language basics• Modules: .erl files ~ similar to .java files ‣ .beam is the compiled module extension ~ similar to .class in java ‣ -module(module_name). ~ similar to public class in java ‣ -export([list of public functions]). ~ similar to public method modifier in java ‣ -import(module,[list of public functions]). ~ similar to import in java ‣ other module attributes: -behaviour(otp behavior pattern name). -record(record_name, tuple). -compile(options).
  • erlang language basics• Functions ~ similar to methods in java ‣ function_name( arguments ) -> function body. ‣ function body consists of instructions separated by commas ‣ one of the instructions results to the function output ‣ BIFs - build in functions ‣ function is called as module_name:function_name(arguments)
  • erlang language basics• Processes ‣ the building blocks of concurrency ‣ simple to create; erlang:system_info(process_limit) 32,767 ‣ simple primitives: spawn, send, receive: Pid = spawn(Node,Fun). Pid ! Message. receive ... end. ‣ Links, Exit signals, System processes spawn_link(Node,Fun) -> Pid process_flag(trap_exit,true). exit(Pid, Why)->true. erlang:monitor(process,Item)->MonitorRef ‣ Registered processes ‣ Independent stack, message queue, garbage collection
  • erlang language basics• Messages ‣ asynchronous ‣ pass by copying ‣ lightweight and simple due to simple data structures ‣ selective receive due to pattern matching: loop(Name,Mod,OldState)-> receive {From,{swap_code,NewMod}}-> From!{Name,acknowledged}, loop(Name,NewMod,OldState); {From,Request}-> {Response,NewState}=Mod:handle(Request,OldState), From!{Name,Response}, loop(Name,Mod,NewState) end.
  • erlang language basics • Terms‣ Lists: Books = [Book1,Book2,Book3] Books = [Book1 | RestOfBooks] Book1 = {{isbn,”1-9343560-0-X”},{title,”Programming Erlang”},{authors,{“Joe Armstrong”}},{pages,515},{publisher,”The Pragmatic Programmer”}} Book2 = {{isbn,”9781933988788”},{title,”Erlang and OTP in Action”}, {authors,{“Martin Logan”,”Eric Merritt”,”Richard Carlson”}},{pages,397}, {publisher,”Manning”}} lists:map(fun(X) ->2*X end, L). [2*X || X<-L]. -list comprehensions‣ Tuples: Book1 = {{isbn,”1-9343560-0-X”},{title,”Programming Erlang”},{authors,{“Joe Armstrong”}},{pages,515},{publisher,”The Pragmatic Programmer”}} {_,{_,Book1_title},_,_,_}=Book1.‣ Funs: Double = fun(X)->2*X end.
  • erlang language basics • Terms‣ Atoms, represent the static constants; lowercase letters-and-numbers-and-@ or any characters within single-quotes Book1 = {{isbn,”1-9343560-0-X”},{title,”Programming Erlang”},{authors,{“Joe Armstrong”}},{pages,515},{publisher,”The Pragmatic Programmer”}}‣ Numbers: integers - limited by available memory, and floats - 64-bit format‣ Strings: list of integers, within double-quotes‣ Binaries: efficient to store large quantity of data, in form of sequence of unsigned 8- bit integers; BIFs that can manipulate binaries; Bit syntax that can pattern match against binaries. <<0,1,...,255>>‣ Term comparisons: number<atom<reference<fun<port<pid<tuple<list<binary
  • erlang language basics • Variables‣ single assignment, immutable, name must start with uppercase letter‣ X=X+1 does not work in Erlang‣ scope is the lexical unit in which it is defined
  • erlang language basics• Recursion ‣ looping ‣ tail recursion efficiency (a) sum(0) -> 0; sum(N) -> sum(N-1)+N. (b) do_sum(0, Total) -> Total; do_sum(N, Total) -> do_sum(N-1, Total+N).
  • erlang language basics• Case expressions case Expression of Pattern1 [when Guard1] -> Expr_seq1; Pattern2 [when Guard2] -> Expr_seq2; ... end• If expressions if Guard1 -> Expr_seq1; Guard2 -> Expr_seq2; ... end• Exceptions try { block } catch (exception) { block } catch (exception) { block } ... finally { block }
  • erlang language basics • OTP patterns‣ library of solutions for common problems ~ similar to an App Server container in JEE‣ it is part of Erlang standard download‣ can be utilized by using -behaviour(pattern_name)‣ common patterns: -behaviour(gen_server). -behaviour(gen_event). -behaviour(supervisor). -behaviour(application).
  • erlang language basics • Mnesia‣ distributed dbms that comes incorporated in the standard Erlang download‣ can store any type of Erlang data structures‣ can be configured to have both memory and disk storage‣ examples: -record(books,{id, book}). B=#books{id={“1-9343560-0-X”}, book = {{isbn, “1-9343560-0-X”},{title, “Programming Erlang”}, {authors,{“Joe Armstrong”}},{pages,515},{publisher, “The Pragmatic Programmer”}} }
  • erlang language basics • Jinterface‣ Java simulation of Erlang‣ a mapping implementation of Erlang language elements into Java‣ java erlang-simulated nodes can talk to the erlang nodes using erlang protocol
  • speculations• sequential and parallel programming - two dimensions equally important in our problem solving• concurrency might no longer remain an attribute of special problems• spiral of change might be introducing some new forces on the OO paradigm