SlideShare ist ein Scribd-Unternehmen logo
1 von 35
Downloaden Sie, um offline zu lesen
Introducing Scala




Developing a new Scala DSL
     for Apache Camel
Goals
●
    Goals
    –   Introduce a few basic concepts/syntax of Scala
    –   How to use these Scala techniques for building a
        Scala DSL (using Apache Camel as an example)
Planning
●
    Introduction
●
    Scala for DSL building
    –   Implicit conversion
    –   Passing functions as parameters
    –   By-name parameters and currying
    –   Caveats
●
    Scala tooling
    –   Maven plugin
    –   Eclipse plugin
Planning
●
    Introduction
●
    Scala for DSL building
    –   Implicit conversion
    –   Passing functions as parameters
    –   By-name parameters and currying
    –   Caveats
●
    Scala tooling
    –   Maven plugin
    –   Eclipse plugin
Introduction
●
    Who am I?
    –   Gert Vanthienen (gert@anova.be)
    –   Independent consultant
         ●
             Open-source (Java/J2EE) technology
         ●
             Legacy integration (System i aka AS/400)
    –   Open-source
         ●
             Apache ServiceMix committer / PMC member
         ●
             Contributor to Apache Camel
Introduction
●
    What is Apache Camel?
    –   Spring-based Integration Framework
    –   Implements enterprise integration patterns
    –   Configured through
         ●
             Java DSL (fluent API)
         ●
             Spring XML configuration file
    –   URIs to work with other transports/protocols
    –   Routing/mediation for ServiceMix, ActiveMQ, CXF, ...
    –   Check out Bruce Snyder's presentation on Friday!!
Introduction
●
    Just a small example of the Java DSL



public class FleetRouteBuilder extends RouteBuilder {

    public void configure() throws Exception {
      from("ftp://server.local:10021/traces/out")
        .to("ftp://server.remote/folder/to/upload")
        .splitter(xpath("/traces/trace"))
        .to("activemq:MY.TRACE.QUEUE")
        .filter(xpath("/trace/@type == 'XYZ'"))
          .to("wmq:QLIN.TRACE.QUEUE");
    }

}
Introduction
●
    What is Scala?
    –   Sca(lable) la(nguage)
    –   Multi-paradigm:
         ●
             Object-oriented: classes, polymorphism, inheritance, ..
         ●
             Functional: function = value, pattern matching, ...
    –   Static typing, using type inference
    –   Interoperates with JRE (and .NET CLR)
         ●
             Scala code compiles into Java bytecode
         ●
             You can call Java code from Scala (and vica versa)
Introduction
●
    A simple Scala class example

    class Person(name: String, age: Int) {

        def eat(food: String) {
          println("Eating " + food + " now")
        }

        def isToddler = age > 0 && age < 3

        override def toString() = "Person[" + name + "]"

    }
Planning
●
    Introduction
●
    Scala language
    –   Implicit conversion
    –   Passing functions as parameters
    –   By-name parameters and currying
    –   Caveats
●
    Scala tooling
    –   Maven plugin
    –   Eclipse plugin
Simple route example
●
    Example of the simplest route possible in Java
    Just receive a message and forward it

    public class MyRouteBuilder extends RouteBuilder {

        public void configure() throws Exception {
          from("direct:a").to("mock:a");
          from("direct:b").to("mock:b");
        }

    }
Simple route example
●
    In the Scala DSL it looks like this...
        class MyRouteBuilder extends RouteBuilder {

            "direct:a" to "mock:a"
            "direct:b" --> "mock:b"

        }

●
    ... using these language features
    –   constructor statements go in the class body
    –   no need for parentheses, dots and semicolons
    –   an operator is implemented like any other method
    –   implicit conversion
Implicit conversion
●
        Strings like “direct:a” and “direct:b” don't have the
        necessary methods (→ and to)
●
        String is final so it can't be subclassed
●
        Using implicit conversion to 'add' the missing
        methods

    class RouteBuilder {

        implicit def stringToUri(uri:String) =
                                        new RichUriString(uri, this)

    }
Implicit conversion
●
        Let's look at the RichUriString
        –   Primary constructor is in class declaration
        –   Defines two methods (return type inference)



    class RichUriString(uri:String, builder:RouteBuilder) {

        def to(target: String) = builder.from(uri).to(target)
        def -->(target: String) = to(target)

    }
Implicit conversion
●
        The full Scala RouteBuilder class

    package org.apache.camel.scala.dsl

    class RouteBuilder {

        val builder = new org.apache.camel.builder.RouteBuilder() {
          override def configure() = {}
        }

        def from(uri: String) = builder.from(uri)

        implicit def stringToUri(uri:String) =
                                        new RichUriString(uri, this)

    }
Implicit conversion
●
    There are a few subtle rules that can bite you when
    using implicit conversion
    –   marking rule
    –   scope rule
    –   explicits-first rule
    –   one-at-a-time rule
    –   non-ambiguity rule
        Example: filter method on ProcessorType/RichString
Filter route example
●
    Java DSL filter looks like this

    public class MyRouteBuilder extends RouteBuilder {

        public void configure() throws Exception {
          from("direct:a").
            filter(body().isEqualTo("<hello/>")).to("mock:a");
        }

    }
Filter route example
●
    In the Scala DSL

    class FilterRouteBuilder extends RouteBuilder {

        "direct:a" when(_.in == "<hello/>") to "mock:a"

    }


●
    Scala language features
        –   passing functions as parameters
        –   equals() in Java becomes == in Scala
Passing functions as parameters
●
        Scala is a functional language
        –   functions are variables
        –   you can pass functions as method parameters
●
        Let's pass a function to the when() method

    class RichUriString(uri: String, builder: RouteBuilder) {

        def when(test: Exchange => Boolean) =
          builder.from(uri).filter(new WhenPredicate(test))


    }
Passing functions as parameters
●
        Predicate<E> is an interface in the Camel API
        –   WhenPredicate is a Scala class that implements it
        –   Use the function with an Exchange to evaluate


    package org.apache.camel.scala.dsl

    class WhenPredicate(function: Exchange => Boolean)
                                       extends Predicate[Exchange]{

        override def matches(exchange: Exchange) = function(exchange)

        //assertMatches is also here

    }
Passing functions as parameters
●
    Passing a function literal in the RouteBuilder
class FilterRouteBuilder extends RouteBuilder {

    "direct:a" when(
      (exchange:Exchange) => exchange.in == "<hello/>"
                                           ) to "mock:a"

}

●
    Shorthand notation
     –   with parameter type inference...
           exchange => exchange.in == "<hello/>"
     –   and placeholders
           _.in == "<hello/>"
CBR example
●
    Java DSL for a simple content-based router

public class MyRouteBuilder extends RouteBuilder {

    public void configure() throws Exception {
      from("direct:a")
        .to("mock:polyglot")
        .choice()
          .when(body().isEqualTo("<hallo/>"))
            .to("mock:dutch")
            .to("mock:german");
          .when(body().isEqualTo("<hello/>")).to("mock:english")
          .otherwise().to("mock:french");

    }

}
CBR example
●
    Scala DSL adds code blocks for supporting more
    advanced route definitions
class CBRRouteBuilder extends RouteBuilder {

    "direct:a" ==> {
      to ("mock:polyglot")
      choice {
        when (_.in == "<hello/>") to ("mock:english")
        when (_.in == "<hallo/>") {
          to ("mock:dutch")
          to ("mock:german")
        }
        otherwise to ("mock:french")
      }
    }

}
By-name parameters and currying
●
        By-name parameters allow you to just pass a block
         of code that takes no parameters


    class RouteBuilder {

        //instead of : def choice(block: () => Unit)
        def choice(block: => Unit) = {
          //just execute the block (no parentheses)
          block
        }

    }
By-name parameters and currying
●
        Currying allows you to use a method that takes
        multiple arguments lists


    class RouteBuilder {

        //snip

        def when(test: Exchange => Boolean)(block: => Unit) = {
          val when = choice.when(new WhenPredicate(test))
          build(when, block)
        }

    }
Caveats
●
    Interaction between Java and Scala generics
●
    Java varargs versus Scala repeated parameters
●
    Operator precedence
Operator precedence
●
    Scala allows you to override operators or declare
    symbol named methods
        –   precedence is determined on the first character

    class SimpleRouteBuilder extends RouteBuilder {

        //these are all the same
        "direct:a" to "mock:a1" to "mock:a2"
        "direct:b" --> "mock:b1" --> "mock:b2"
        "direct:c" --> "mock:c1" to "mock:c2"

        //but this is something entirely different
        "direct:d" to "mock:d1" --> "mock:d2"

    }
Java/Scala generics
●
     Most of the times, you can simply replace <> by []
●
     A Java type defined as...
    public class ProcessorType<Type extends ProcessorType> {}


●
     In Java, you can also declare the raw type ...
     (you'll only get compiler warnings)
●
     ... but in Scala this doesn't work. The solution is
     this (ugly-looking) syntax (existential type).
    implicit def processorWrapper(
                 processor: ProcessorType[T] forSome {type T}) =
                                      new RichProcessor(processor)
Varargs/repeated parameters
●
    Java varargs...
     public Type to(String... uri) {
        //does some work
     }
●
    ... are like Scala repeated parameters
     def to(uris: String*) = //implementation goes here


●
    Caveats:
     def to(uris: String*) = {
       val processor = builder.from(uri)
       processor.to(uris.toArray[String])
     }

     def -->(uris: String*) = to(uris:_*)
Other language features
●
    What else is there?
    –   traits and mixins
    –   pattern matching
    –   partially applied functions
    –   apply() and unapply()
    –   language support for XML
        XML literals, pattern matching for XML, ...
    –   actors
    –   annotation support
    –   ...
Planning
●
    Introduction
●
    Scala for DSL building
    –   Implicit conversion
    –   Passing functions as parameters
    –   By-name parameters and currying
    –   Caveats
●
    Scala tooling
    –   Maven plugin
    –   Eclipse plugin
Scala Maven plugin
●
    Integrate Scala in your current Maven build
    –   http://scala-tools.org/mvnsites/maven-scala-plugin/
    –   specify repository and plugin
    –   also need to specify source/test folders
●
    Other features
    –   continuous compilation (scala:cc)
    –   scaladoc generation (scala:doc)
    –   scala interactive console (scala:console)
Scala Eclipse plugin
●
    Scala plugin for Eclipse
    http://www.scala-lang.org/tools/eclipse/
    –   Scala development perspective
    –   Syntax highlighting and formatting
    –   Wizards for classes, traits, objects, ...
●
    But...
    –   If you have problems, resort to manual building
        (Ctrl-B)
    –   Occasionally, you may have to clean your project to
        get up-to-date compile messages
Scala Eclipse plugin
●
    Configuring Maven Eclipse plugin to generate Scala
    project descriptors
    –   add a nature:
        ch.epfl.lamp.sdt.core.scalanature
    –   add a builder:
        ch.epfl.lamp.sdt.core.scalabuilder
    –   add a build classpath container:
        ch.epfl.lamp.sdt.launching.SCALA_CONTAINER
Thanks for attending...




   Questions? Remarks?

Weitere ähnliche Inhalte

Was ist angesagt?

Core Java Certification
Core Java CertificationCore Java Certification
Core Java CertificationVskills
 
Basic info on java intro
Basic info on java introBasic info on java intro
Basic info on java introkabirmahlotra
 
Scala overview
Scala overviewScala overview
Scala overviewSteve Min
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScriptRangana Sampath
 
An Introduction to JavaScript
An Introduction to JavaScriptAn Introduction to JavaScript
An Introduction to JavaScripttonyh1
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)arvind pandey
 
Have Your Cake and Eat It Too: Meta-Programming Techniques for Java
Have Your Cake and Eat It Too: Meta-Programming Techniques for JavaHave Your Cake and Eat It Too: Meta-Programming Techniques for Java
Have Your Cake and Eat It Too: Meta-Programming Techniques for JavaHoward Lewis Ship
 
Scala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud FoundryScala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud FoundryPray Desai
 
Scala: functional programming for the imperative mind
Scala: functional programming for the imperative mindScala: functional programming for the imperative mind
Scala: functional programming for the imperative mindSander Mak (@Sander_Mak)
 
From Ruby to Scala
From Ruby to ScalaFrom Ruby to Scala
From Ruby to Scalatod esking
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced JavascriptDhruvin Shah
 

Was ist angesagt? (14)

Core Java Certification
Core Java CertificationCore Java Certification
Core Java Certification
 
Basic info on java intro
Basic info on java introBasic info on java intro
Basic info on java intro
 
Scala overview
Scala overviewScala overview
Scala overview
 
Core Java Tutorial
Core Java TutorialCore Java Tutorial
Core Java Tutorial
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
An Introduction to JavaScript
An Introduction to JavaScriptAn Introduction to JavaScript
An Introduction to JavaScript
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
 
Have Your Cake and Eat It Too: Meta-Programming Techniques for Java
Have Your Cake and Eat It Too: Meta-Programming Techniques for JavaHave Your Cake and Eat It Too: Meta-Programming Techniques for Java
Have Your Cake and Eat It Too: Meta-Programming Techniques for Java
 
JS - Basics
JS - BasicsJS - Basics
JS - Basics
 
Scala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud FoundryScala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud Foundry
 
Scala: functional programming for the imperative mind
Scala: functional programming for the imperative mindScala: functional programming for the imperative mind
Scala: functional programming for the imperative mind
 
From Ruby to Scala
From Ruby to ScalaFrom Ruby to Scala
From Ruby to Scala
 
Core java
Core javaCore java
Core java
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
 

Ähnlich wie Camel Scala

BCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java DevelopersBCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java DevelopersMiles Sabin
 
Angular for Java Enterprise Developers: Oracle Code One 2018
Angular for Java Enterprise Developers: Oracle Code One 2018Angular for Java Enterprise Developers: Oracle Code One 2018
Angular for Java Enterprise Developers: Oracle Code One 2018Loiane Groner
 
Short intro to scala and the play framework
Short intro to scala and the play frameworkShort intro to scala and the play framework
Short intro to scala and the play frameworkFelipe
 
Spring Day | Spring and Scala | Eberhard Wolff
Spring Day | Spring and Scala | Eberhard WolffSpring Day | Spring and Scala | Eberhard Wolff
Spring Day | Spring and Scala | Eberhard WolffJAX London
 
java: basics, user input, data type, constructor
java:  basics, user input, data type, constructorjava:  basics, user input, data type, constructor
java: basics, user input, data type, constructorShivam Singhal
 
Qcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_scalaQcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_scalaMichael Stal
 
Scala for Java Programmers
Scala for Java ProgrammersScala for Java Programmers
Scala for Java ProgrammersEric Pederson
 
OpenDaylight and YANG
OpenDaylight and YANGOpenDaylight and YANG
OpenDaylight and YANGCoreStack
 
Railroading into Scala
Railroading into ScalaRailroading into Scala
Railroading into ScalaNehal Shah
 
A Brief Introduction to Scala for Java Developers
A Brief Introduction to Scala for Java DevelopersA Brief Introduction to Scala for Java Developers
A Brief Introduction to Scala for Java DevelopersMiles Sabin
 
Miles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java DevelopersMiles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java DevelopersSkills Matter
 
Introduction to Scala | Big Data Hadoop Spark Tutorial | CloudxLab
Introduction to Scala | Big Data Hadoop Spark Tutorial | CloudxLabIntroduction to Scala | Big Data Hadoop Spark Tutorial | CloudxLab
Introduction to Scala | Big Data Hadoop Spark Tutorial | CloudxLabCloudxLab
 
Java basic tutorial
Java basic tutorialJava basic tutorial
Java basic tutorialBui Kiet
 
How AngularJS Embraced Traditional Design Patterns
How AngularJS Embraced Traditional Design PatternsHow AngularJS Embraced Traditional Design Patterns
How AngularJS Embraced Traditional Design PatternsRan Mizrahi
 
Core Java Programming Language (JSE) : Chapter X - I/O Fundamentals
Core Java Programming Language (JSE) : Chapter X - I/O Fundamentals Core Java Programming Language (JSE) : Chapter X - I/O Fundamentals
Core Java Programming Language (JSE) : Chapter X - I/O Fundamentals WebStackAcademy
 

Ähnlich wie Camel Scala (20)

BCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java DevelopersBCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java Developers
 
Angular for Java Enterprise Developers: Oracle Code One 2018
Angular for Java Enterprise Developers: Oracle Code One 2018Angular for Java Enterprise Developers: Oracle Code One 2018
Angular for Java Enterprise Developers: Oracle Code One 2018
 
Short intro to scala and the play framework
Short intro to scala and the play frameworkShort intro to scala and the play framework
Short intro to scala and the play framework
 
Spring Day | Spring and Scala | Eberhard Wolff
Spring Day | Spring and Scala | Eberhard WolffSpring Day | Spring and Scala | Eberhard Wolff
Spring Day | Spring and Scala | Eberhard Wolff
 
Scala and Spring
Scala and SpringScala and Spring
Scala and Spring
 
java: basics, user input, data type, constructor
java:  basics, user input, data type, constructorjava:  basics, user input, data type, constructor
java: basics, user input, data type, constructor
 
Qcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_scalaQcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_scala
 
Scala for Java Programmers
Scala for Java ProgrammersScala for Java Programmers
Scala for Java Programmers
 
OpenDaylight and YANG
OpenDaylight and YANGOpenDaylight and YANG
OpenDaylight and YANG
 
Railroading into Scala
Railroading into ScalaRailroading into Scala
Railroading into Scala
 
A Brief Introduction to Scala for Java Developers
A Brief Introduction to Scala for Java DevelopersA Brief Introduction to Scala for Java Developers
A Brief Introduction to Scala for Java Developers
 
Miles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java DevelopersMiles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java Developers
 
Introduction to Scala | Big Data Hadoop Spark Tutorial | CloudxLab
Introduction to Scala | Big Data Hadoop Spark Tutorial | CloudxLabIntroduction to Scala | Big Data Hadoop Spark Tutorial | CloudxLab
Introduction to Scala | Big Data Hadoop Spark Tutorial | CloudxLab
 
Java
JavaJava
Java
 
Java basic
Java basicJava basic
Java basic
 
Java basic tutorial
Java basic tutorialJava basic tutorial
Java basic tutorial
 
Java Basics
Java BasicsJava Basics
Java Basics
 
How AngularJS Embraced Traditional Design Patterns
How AngularJS Embraced Traditional Design PatternsHow AngularJS Embraced Traditional Design Patterns
How AngularJS Embraced Traditional Design Patterns
 
Scala in Places API
Scala in Places APIScala in Places API
Scala in Places API
 
Core Java Programming Language (JSE) : Chapter X - I/O Fundamentals
Core Java Programming Language (JSE) : Chapter X - I/O Fundamentals Core Java Programming Language (JSE) : Chapter X - I/O Fundamentals
Core Java Programming Language (JSE) : Chapter X - I/O Fundamentals
 

Mehr von elliando dias

Clojurescript slides
Clojurescript slidesClojurescript slides
Clojurescript slideselliando dias
 
Why you should be excited about ClojureScript
Why you should be excited about ClojureScriptWhy you should be excited about ClojureScript
Why you should be excited about ClojureScriptelliando dias
 
Functional Programming with Immutable Data Structures
Functional Programming with Immutable Data StructuresFunctional Programming with Immutable Data Structures
Functional Programming with Immutable Data Structureselliando dias
 
Nomenclatura e peças de container
Nomenclatura  e peças de containerNomenclatura  e peças de container
Nomenclatura e peças de containerelliando dias
 
Polyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better AgilityPolyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better Agilityelliando dias
 
Javascript Libraries
Javascript LibrariesJavascript Libraries
Javascript Librarieselliando dias
 
How to Make an Eight Bit Computer and Save the World!
How to Make an Eight Bit Computer and Save the World!How to Make an Eight Bit Computer and Save the World!
How to Make an Eight Bit Computer and Save the World!elliando dias
 
A Practical Guide to Connecting Hardware to the Web
A Practical Guide to Connecting Hardware to the WebA Practical Guide to Connecting Hardware to the Web
A Practical Guide to Connecting Hardware to the Webelliando dias
 
Introdução ao Arduino
Introdução ao ArduinoIntrodução ao Arduino
Introdução ao Arduinoelliando dias
 
Incanter Data Sorcery
Incanter Data SorceryIncanter Data Sorcery
Incanter Data Sorceryelliando dias
 
Fab.in.a.box - Fab Academy: Machine Design
Fab.in.a.box - Fab Academy: Machine DesignFab.in.a.box - Fab Academy: Machine Design
Fab.in.a.box - Fab Academy: Machine Designelliando dias
 
The Digital Revolution: Machines that makes
The Digital Revolution: Machines that makesThe Digital Revolution: Machines that makes
The Digital Revolution: Machines that makeselliando dias
 
Hadoop - Simple. Scalable.
Hadoop - Simple. Scalable.Hadoop - Simple. Scalable.
Hadoop - Simple. Scalable.elliando dias
 
Hadoop and Hive Development at Facebook
Hadoop and Hive Development at FacebookHadoop and Hive Development at Facebook
Hadoop and Hive Development at Facebookelliando dias
 
Multi-core Parallelization in Clojure - a Case Study
Multi-core Parallelization in Clojure - a Case StudyMulti-core Parallelization in Clojure - a Case Study
Multi-core Parallelization in Clojure - a Case Studyelliando dias
 

Mehr von elliando dias (20)

Clojurescript slides
Clojurescript slidesClojurescript slides
Clojurescript slides
 
Why you should be excited about ClojureScript
Why you should be excited about ClojureScriptWhy you should be excited about ClojureScript
Why you should be excited about ClojureScript
 
Functional Programming with Immutable Data Structures
Functional Programming with Immutable Data StructuresFunctional Programming with Immutable Data Structures
Functional Programming with Immutable Data Structures
 
Nomenclatura e peças de container
Nomenclatura  e peças de containerNomenclatura  e peças de container
Nomenclatura e peças de container
 
Geometria Projetiva
Geometria ProjetivaGeometria Projetiva
Geometria Projetiva
 
Polyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better AgilityPolyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better Agility
 
Javascript Libraries
Javascript LibrariesJavascript Libraries
Javascript Libraries
 
How to Make an Eight Bit Computer and Save the World!
How to Make an Eight Bit Computer and Save the World!How to Make an Eight Bit Computer and Save the World!
How to Make an Eight Bit Computer and Save the World!
 
Ragel talk
Ragel talkRagel talk
Ragel talk
 
A Practical Guide to Connecting Hardware to the Web
A Practical Guide to Connecting Hardware to the WebA Practical Guide to Connecting Hardware to the Web
A Practical Guide to Connecting Hardware to the Web
 
Introdução ao Arduino
Introdução ao ArduinoIntrodução ao Arduino
Introdução ao Arduino
 
Minicurso arduino
Minicurso arduinoMinicurso arduino
Minicurso arduino
 
Incanter Data Sorcery
Incanter Data SorceryIncanter Data Sorcery
Incanter Data Sorcery
 
Rango
RangoRango
Rango
 
Fab.in.a.box - Fab Academy: Machine Design
Fab.in.a.box - Fab Academy: Machine DesignFab.in.a.box - Fab Academy: Machine Design
Fab.in.a.box - Fab Academy: Machine Design
 
The Digital Revolution: Machines that makes
The Digital Revolution: Machines that makesThe Digital Revolution: Machines that makes
The Digital Revolution: Machines that makes
 
Hadoop + Clojure
Hadoop + ClojureHadoop + Clojure
Hadoop + Clojure
 
Hadoop - Simple. Scalable.
Hadoop - Simple. Scalable.Hadoop - Simple. Scalable.
Hadoop - Simple. Scalable.
 
Hadoop and Hive Development at Facebook
Hadoop and Hive Development at FacebookHadoop and Hive Development at Facebook
Hadoop and Hive Development at Facebook
 
Multi-core Parallelization in Clojure - a Case Study
Multi-core Parallelization in Clojure - a Case StudyMulti-core Parallelization in Clojure - a Case Study
Multi-core Parallelization in Clojure - a Case Study
 

Kürzlich hochgeladen

Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 

Kürzlich hochgeladen (20)

Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 

Camel Scala

  • 1. Introducing Scala Developing a new Scala DSL for Apache Camel
  • 2. Goals ● Goals – Introduce a few basic concepts/syntax of Scala – How to use these Scala techniques for building a Scala DSL (using Apache Camel as an example)
  • 3. Planning ● Introduction ● Scala for DSL building – Implicit conversion – Passing functions as parameters – By-name parameters and currying – Caveats ● Scala tooling – Maven plugin – Eclipse plugin
  • 4. Planning ● Introduction ● Scala for DSL building – Implicit conversion – Passing functions as parameters – By-name parameters and currying – Caveats ● Scala tooling – Maven plugin – Eclipse plugin
  • 5. Introduction ● Who am I? – Gert Vanthienen (gert@anova.be) – Independent consultant ● Open-source (Java/J2EE) technology ● Legacy integration (System i aka AS/400) – Open-source ● Apache ServiceMix committer / PMC member ● Contributor to Apache Camel
  • 6. Introduction ● What is Apache Camel? – Spring-based Integration Framework – Implements enterprise integration patterns – Configured through ● Java DSL (fluent API) ● Spring XML configuration file – URIs to work with other transports/protocols – Routing/mediation for ServiceMix, ActiveMQ, CXF, ... – Check out Bruce Snyder's presentation on Friday!!
  • 7. Introduction ● Just a small example of the Java DSL public class FleetRouteBuilder extends RouteBuilder { public void configure() throws Exception { from("ftp://server.local:10021/traces/out") .to("ftp://server.remote/folder/to/upload") .splitter(xpath("/traces/trace")) .to("activemq:MY.TRACE.QUEUE") .filter(xpath("/trace/@type == 'XYZ'")) .to("wmq:QLIN.TRACE.QUEUE"); } }
  • 8. Introduction ● What is Scala? – Sca(lable) la(nguage) – Multi-paradigm: ● Object-oriented: classes, polymorphism, inheritance, .. ● Functional: function = value, pattern matching, ... – Static typing, using type inference – Interoperates with JRE (and .NET CLR) ● Scala code compiles into Java bytecode ● You can call Java code from Scala (and vica versa)
  • 9. Introduction ● A simple Scala class example class Person(name: String, age: Int) { def eat(food: String) { println("Eating " + food + " now") } def isToddler = age > 0 && age < 3 override def toString() = "Person[" + name + "]" }
  • 10. Planning ● Introduction ● Scala language – Implicit conversion – Passing functions as parameters – By-name parameters and currying – Caveats ● Scala tooling – Maven plugin – Eclipse plugin
  • 11. Simple route example ● Example of the simplest route possible in Java Just receive a message and forward it public class MyRouteBuilder extends RouteBuilder { public void configure() throws Exception { from("direct:a").to("mock:a"); from("direct:b").to("mock:b"); } }
  • 12. Simple route example ● In the Scala DSL it looks like this... class MyRouteBuilder extends RouteBuilder { "direct:a" to "mock:a" "direct:b" --> "mock:b" } ● ... using these language features – constructor statements go in the class body – no need for parentheses, dots and semicolons – an operator is implemented like any other method – implicit conversion
  • 13. Implicit conversion ● Strings like “direct:a” and “direct:b” don't have the necessary methods (→ and to) ● String is final so it can't be subclassed ● Using implicit conversion to 'add' the missing methods class RouteBuilder { implicit def stringToUri(uri:String) = new RichUriString(uri, this) }
  • 14. Implicit conversion ● Let's look at the RichUriString – Primary constructor is in class declaration – Defines two methods (return type inference) class RichUriString(uri:String, builder:RouteBuilder) { def to(target: String) = builder.from(uri).to(target) def -->(target: String) = to(target) }
  • 15. Implicit conversion ● The full Scala RouteBuilder class package org.apache.camel.scala.dsl class RouteBuilder { val builder = new org.apache.camel.builder.RouteBuilder() { override def configure() = {} } def from(uri: String) = builder.from(uri) implicit def stringToUri(uri:String) = new RichUriString(uri, this) }
  • 16. Implicit conversion ● There are a few subtle rules that can bite you when using implicit conversion – marking rule – scope rule – explicits-first rule – one-at-a-time rule – non-ambiguity rule Example: filter method on ProcessorType/RichString
  • 17. Filter route example ● Java DSL filter looks like this public class MyRouteBuilder extends RouteBuilder { public void configure() throws Exception { from("direct:a"). filter(body().isEqualTo("<hello/>")).to("mock:a"); } }
  • 18. Filter route example ● In the Scala DSL class FilterRouteBuilder extends RouteBuilder { "direct:a" when(_.in == "<hello/>") to "mock:a" } ● Scala language features – passing functions as parameters – equals() in Java becomes == in Scala
  • 19. Passing functions as parameters ● Scala is a functional language – functions are variables – you can pass functions as method parameters ● Let's pass a function to the when() method class RichUriString(uri: String, builder: RouteBuilder) { def when(test: Exchange => Boolean) = builder.from(uri).filter(new WhenPredicate(test)) }
  • 20. Passing functions as parameters ● Predicate<E> is an interface in the Camel API – WhenPredicate is a Scala class that implements it – Use the function with an Exchange to evaluate package org.apache.camel.scala.dsl class WhenPredicate(function: Exchange => Boolean) extends Predicate[Exchange]{ override def matches(exchange: Exchange) = function(exchange) //assertMatches is also here }
  • 21. Passing functions as parameters ● Passing a function literal in the RouteBuilder class FilterRouteBuilder extends RouteBuilder { "direct:a" when( (exchange:Exchange) => exchange.in == "<hello/>" ) to "mock:a" } ● Shorthand notation – with parameter type inference... exchange => exchange.in == "<hello/>" – and placeholders _.in == "<hello/>"
  • 22. CBR example ● Java DSL for a simple content-based router public class MyRouteBuilder extends RouteBuilder { public void configure() throws Exception { from("direct:a") .to("mock:polyglot") .choice() .when(body().isEqualTo("<hallo/>")) .to("mock:dutch") .to("mock:german"); .when(body().isEqualTo("<hello/>")).to("mock:english") .otherwise().to("mock:french"); } }
  • 23. CBR example ● Scala DSL adds code blocks for supporting more advanced route definitions class CBRRouteBuilder extends RouteBuilder { "direct:a" ==> { to ("mock:polyglot") choice { when (_.in == "<hello/>") to ("mock:english") when (_.in == "<hallo/>") { to ("mock:dutch") to ("mock:german") } otherwise to ("mock:french") } } }
  • 24. By-name parameters and currying ● By-name parameters allow you to just pass a block of code that takes no parameters class RouteBuilder { //instead of : def choice(block: () => Unit) def choice(block: => Unit) = { //just execute the block (no parentheses) block } }
  • 25. By-name parameters and currying ● Currying allows you to use a method that takes multiple arguments lists class RouteBuilder { //snip def when(test: Exchange => Boolean)(block: => Unit) = { val when = choice.when(new WhenPredicate(test)) build(when, block) } }
  • 26. Caveats ● Interaction between Java and Scala generics ● Java varargs versus Scala repeated parameters ● Operator precedence
  • 27. Operator precedence ● Scala allows you to override operators or declare symbol named methods – precedence is determined on the first character class SimpleRouteBuilder extends RouteBuilder { //these are all the same "direct:a" to "mock:a1" to "mock:a2" "direct:b" --> "mock:b1" --> "mock:b2" "direct:c" --> "mock:c1" to "mock:c2" //but this is something entirely different "direct:d" to "mock:d1" --> "mock:d2" }
  • 28. Java/Scala generics ● Most of the times, you can simply replace <> by [] ● A Java type defined as... public class ProcessorType<Type extends ProcessorType> {} ● In Java, you can also declare the raw type ... (you'll only get compiler warnings) ● ... but in Scala this doesn't work. The solution is this (ugly-looking) syntax (existential type). implicit def processorWrapper( processor: ProcessorType[T] forSome {type T}) = new RichProcessor(processor)
  • 29. Varargs/repeated parameters ● Java varargs... public Type to(String... uri) { //does some work } ● ... are like Scala repeated parameters def to(uris: String*) = //implementation goes here ● Caveats: def to(uris: String*) = { val processor = builder.from(uri) processor.to(uris.toArray[String]) } def -->(uris: String*) = to(uris:_*)
  • 30. Other language features ● What else is there? – traits and mixins – pattern matching – partially applied functions – apply() and unapply() – language support for XML XML literals, pattern matching for XML, ... – actors – annotation support – ...
  • 31. Planning ● Introduction ● Scala for DSL building – Implicit conversion – Passing functions as parameters – By-name parameters and currying – Caveats ● Scala tooling – Maven plugin – Eclipse plugin
  • 32. Scala Maven plugin ● Integrate Scala in your current Maven build – http://scala-tools.org/mvnsites/maven-scala-plugin/ – specify repository and plugin – also need to specify source/test folders ● Other features – continuous compilation (scala:cc) – scaladoc generation (scala:doc) – scala interactive console (scala:console)
  • 33. Scala Eclipse plugin ● Scala plugin for Eclipse http://www.scala-lang.org/tools/eclipse/ – Scala development perspective – Syntax highlighting and formatting – Wizards for classes, traits, objects, ... ● But... – If you have problems, resort to manual building (Ctrl-B) – Occasionally, you may have to clean your project to get up-to-date compile messages
  • 34. Scala Eclipse plugin ● Configuring Maven Eclipse plugin to generate Scala project descriptors – add a nature: ch.epfl.lamp.sdt.core.scalanature – add a builder: ch.epfl.lamp.sdt.core.scalabuilder – add a build classpath container: ch.epfl.lamp.sdt.launching.SCALA_CONTAINER
  • 35. Thanks for attending... Questions? Remarks?