Wednesday, December 17, 2014

Leverage Spring JavaMailSender's batch send method and gain performance when sending series of emails...

I have recently experienced a performance issue in a module that is coded to send email notifications to multiple recipients using Spring Java Mail support. The email text for each recipient was different and hence it was initially coded to loop through all recipients, prepare MimeMessage with the help of an anonymous inner class of MimeMessagePreparator for each recipient, and call JavaMailSender's send(MimeMessagePreparator mimeMessagePreparator) method. For every message the send method was taking about 20 secs or so for the call to finish. When there about 20 recipients, there were 20 such calls to this method and the delay was noticeable in minutes.

While reading through the Javadoc of Spring classes, I found that there is another send method in JavaMailSender class which takes an array of MimeMessages as an argument and sends mail in batch mode. I used this method instead and prepared an array of MimeMessages one per each recipient using just the MimeMessageHelper. This improved the performance and for 20 recipients which is 20 emails anyway, it only took about 20 seconds.

Under the covers this method could be using the same mail session to send all MimeMessages in batch mode.

Monday, December 08, 2014

SpringOne2GX-2014 Day-4

Day-4 Sessions

Reactor and Reactive Streams - John Maldini, Stephan Brisbin
Reactor and Reactive Streams - John Maldini, Stephan Brisbin
My notes
  • NanoService, MicroService, NotTooBigService
  • Reactors are going to help across the boundaries, where microservices cross boundaries to interact with other services in order to get the work done. Microservice can be a method call, a db operation, a file operation etc. which has it’s own (thread) boundary.
  • Reactive Programming- Event driven, scalable, low-latency, fault-tolerant
  • Scaling doesn’t mean throwing more and more resources, often times more throughput can be achieved by scaling down the big operations into smaller so that they can go in and out quickly and better throughput can be achieved with smaller number of threads with less latency.
Groovy vampires, Combining Groovy, REST, NoSQL, and more - Kenneth Kousen

 My notes
  • Making Java Groovy Book: Ratpack next edition
  • REST    Addressible resources, Uniform interface (most public facing restful api are restricted to GET requests, read-only web-service), Content Negotiation (content type in url:e.g. JSON, or accept-header, POST req: both request-header and content-header to indicate what you are sending and what you can receive)
  • GET requests in Groovy are trivial: ‘...url...’.toURL().text()
  • s = [k1:v1, k2:v2].collect{k,v-> “$k=$v”}.join(‘&’)
  • s = [k1:v1, k2:v2].collect{it}.join(‘&’) //more groovier
  • JsonOutput.prettyPrint(url.toUrl().text())
    • commands (interactive javascript based console): mongo, show databases, use movies, show collections,
    • Java driver available com.mongodb.BasicDBObject
    • TODO: GMongo Project (Groovy) - take a look at the code, @delegate Mongo mongo, delegates to Java Mongo class
    • GMongo mongo = new GMongo(); def db = db.getdb(‘myDb’); db.myCollection.find()....//etc
    • db.myCollection << collection //populates mongo db collection, if not there it creates and populates
    • mongo db generates ids
  • Parsing JSON and converting to Java Objects
  • GSON, Google JSON to convert JSON to Java Object
  • Groovy is dynamically typed, but strongly typed language. JSON has no type, it’s weakly typed language.
  • Lazybones - Generates project templates
  • gvm : gvm c //gvm current
  • lazybones list //list all types of apps it can generate templates for, ratpack is one in the list
  • TIP:@Singleton : Groovy
  • TIP: @Shared : JUnit and Spock, if you don’t want vars to be re-initialized for each test…
  • TIP:groovy each vs. every: each returns a collection, every returns a boolean that satifies the statement in the closure
  • Grails: Alternative server, REST capabilities, Mongo plugin

Android and Groovy - a winning pair- Cedric Champeau

Android and Groovy - a winning pair- Cedric Champeau
 My notes
  • Why Android? Uses JVM, SDK is free, Tooling is freely available (Android Studio), don’t own a Mac, Swift (Apple) - inspired by Groovy, Scala and many languages
  • Why Groovy: Great features in the language and in the API, Built on top of Giant Java, Android developers shouldn’t be suffering, Java on Android is very verbose, incredibly verbose, tonnes of ceremony
  • Groovy on Android: the problems- Groovy is dynamic, lot of things happen at run-time, Intensive use of reflection, Potentially slow invocation paths, bytecode on Android is different. Multiple runtimes on Android: Dalvik, ART (new: your device becomes a compiler for the application, it compiles into native bytecode, application installation takes time, purely native code and there is no JVM on the device, so the app runs faster), Behavior not the same as the standard JVM
  • Discobot: 2 guys started Groovy on Android in 2011, forking Groovy 1.7. Groovy was not modularized at the time
  • Dex files: Dalvik VM is new bytecode format
  • Groovy 2.4: Android objectives- Main focus on @CompileStatic, Optional use of dynamic Groovy, Support Android in std distribution, Building a full Android application in Groovy, Bring the goodness of Groovy to Android, Opening new doors: Invent new frameworks.
  • Groovy for Android
    • Requirements: Gradle, Android Studio, Groovy 2.4.0-beta-3
    • Groovy 2.4 Android Support: Must use a specific Android jar, replaces java.beans with openbeans, reduced number of methods for 64k limit of dex files, workarounds for Android specific behavior
    • Gradle plugin: groovy-android plugin
    • Demo: an application on Android in Groovy which starts a countdown timer on Android device and syncs up the timer on Android wearable device (watch)
    • Demo: Create an app- Android studio, create project (mobile and wear), add needed stuff to build.gradle, rename the Java file to Groovy, make the code Groovy
  • TIP: @ClosureParams - with this param, you tell the compiler what type of parameter to infer when parameters are passed into closure without have to define the parameter types.
    • Groovifying Android APIs
    • Performance: GR8Conf Agenda app- Groovy jar 4.5MB, appl size: 2MB, After ProGuard only 1MB, 8.2MB of RAM (but lots of images). There is absolutely no difference from Java with the groovy app @CompilerStatic
    • Other Cool things:
      • SwissKnife - @OnClick(R.id.button) kind of method injections - all such tools are based on APT Annotation Processing Tools. In Groovy it’s all through AST
      • Grooid Tools: Builders for Views, experimental
      • Dragger-like dependency injection FW
      • Data binding APIs
      • Improved reactive APIs
  • Future is now: NYT - Next app will be written in Groovy
http://www.infoq.com/presentations/android-groovy-jvm?utm_campaign=infoq_content&utm_source=infoq&utm_medium=feed&utm_term=global

Takeaways for me:
  • Spring Boot
  • Java 8
  • Groovy 2.4 beta 3 for Android
  • Grails 2.3
  • Spock
This was my 4th Spring One conference, my first one was in 2006 and then I attended the ones in 2012 and 2013. There were about 1000+ attendees and it must have been the largest gathering ever, I guess. The food was good, speakers were great and it was a great great experience. I was totally in my professional world for 4 days and enjoyed every bit of it. Looking forward to more and more in future ;)

Have fun with SPRING Frameworks and Groovy! ;)

SpringOne2GX-2014 Day-3

Day-3 Sessions

Microservice Architecture with Grails 2 - Jeff Brown
Microservice Architecture with Grails 2 - Jeff Brown

My notes
  • Microservices, Spring Boot, Grails 3
  • Microservices by Martin Fowler - a good place to start http://martinfowler.com/articles/microservices.html
  • Versioning resources e.g Employee management system, Contract management system as 2 micro services. One system evolves into a different version. Within grails apps the resource can be versioned.
    • “people/v1”(resources:’book’, namespace:’v1’)
    • “people/v2”(resources:’book’, namespace:’v2’)
    • “people”(version: 1.0, resources:’book’, namespace:’v1’) //looks for http header
    • “people”(version: 2.0, resources:’book’, namespace:’v2’) 
    • Controllers can be name-spaced. There can be any number of BookControllers with the same name but in different packages, versioned in the same application.
    • another way Accept-Version:1.0 can be in the header with the same url
    • Default is the highest version  number if left out in the header
  •  Domain class Resource annotation @Resource(uri=’automobiles’) class Car{} //this will create a CarController that extends grails.rest.RestfulController. queryForResource() method is the one called everytime a resource retrieve request is received. It can be overridden. getObjectToBind() is another method you may need to override.
  • REST Client Builder RestBuilder()
    • plugins { compile: “:rest-client-builder:2.0.3” }
  •  TIP: In groovy underscores can be put into numbers: e.g. 200_000
  • 3 small grails applications bank, car, webui, each running on a different ports 8080, 8082, 8084.  When grails starts on a port it uses another post to listen to some details that it needs to get on the running application. e.g when run on 8080, 8081 is taken too.
  • webui app sends a rest req (build a RestBuilder() and send rest request and get json responce) to the bank app and gets the list of banks and displays in a form for searching cars app. Gets the cars price and compares prices and list only cars that the bank can offer loan based on maxLoanPrice that the bank offers.
  • TIP: If you are building apps that do not need any plugin, remove it to make the application size reduced. e.g. Hibernate, The Bankui app is not using hibernate at all.
   
Testing Driven Development with Spock - Venkat Subramaniam
Testing Driven Development with Spock - Venkat Subramaniam
My notes
  • Automated testing: Software crisis, time and disciple, This code is not testable = The design sucks, lack of discipline, lack of knowing how to do it, value it as much it not more than design, how do you know the code that worked before works now, JDD - Jesus Driven development ;)
  • TDD, BDD
  • Spock
    • Groovy to test Java, Spock to test Groovy, spock is groovier than groovy in lot many ways
  • Example:
  • Canary testing : a simple test method expect: true == true ;)
  • spock can be used to test code written in any language for jvm as it uses the compiled code
  • One of the TDD principles: You don’t write code until a test fails ;)
  • Very short feedback cycle. groovysh/groovyconsole to play with code right away. Java repple - being considered for Java 9. They already got it: groovysh ;)
  • multiple unrelated assets in a test method: problem, you don’t know where it failed
  • Spock undrills multiple asserts and gives nice feedback (where: block)
  • where : tabular pattern of test data with | construct or << construct
  • expect: exceptions, kind of exception, exception message etc.
  • expect:
  • Stub vs. Mock. Mock is useful to mockout behavior, stub is useful to stubout the stub
  • Spell checker: Java spell checkers, which one to use? mocking, when you can mock, why to worry about whick spell checker to brng in, integration issues etc.
  • Injections: Constructor based, setter based, factory
  • Create an interface file SpellChecker: boolean isCorrect(String word)
  • void setUp(){ spellChecker = Mock(SpellChecker; scrambler.spellChecker = spellChecker)
  • Mocking: can be problamatic, The more coupling you create, the more mocking you need to do. Use mocking very sparingly. More mocking is a design smell. Use light mocking.
  • TODO: blogged: “Knock out before you mock out”
  • Spock summary: very light, nice, applies AST transformations nicely.
  • Fun: do you use maven? nobody uses maven, maven uses you
Advanced GORM: Beyond relational - Graeme Rocher
Advanced GORM: Beyond relational - Graeme Rocher
My notes
  • GORM Internals
    • Visible to Java code, as it is into bytecode through AST Transformations
  • GORM Standalone
    • Easy initialization of GORM outside grails
    • Boot pluguin
    • @Entity - gorm tag, applies AST transformations and make sthe domain class rich model class
    • GORM for Hinernate standalone
    • DEMO: a simple gradle project with spock tests, grails-datasource-gorm-hibernate dependency
  • Tips & Tricks
    • Where queries, compilation checks in where closure, powerful and flexible.
    • Subqueries
    • Hibernate Column Formula, Column Readers and Writers
    • Asynchronous GORM: in GORM any method can become async
  • Unit testing GORM
    • dependenc: grails-datastore-test-support
    • Provides HibernateTestMixin
    • Annotate spock test spec class with @TestMixin(HibernateTestMixen)
Groovy Mobile Automation - Bobby Warner
My notes
    • Opensource test automation framework
  • Architecture
    • Appium Java client (selinium), Gradle, Groovy, Spock
    • Appium Java client: Wrapper of Selenium 2.x, Uses GSON (Google JSON)
    • Groovy, Spock, Gradle
    • dependency : appium (transitive: selenium)
    • iOS is a simulator, Genymotion emulator is a full android system
    • 2 spock tests testing both iOS and Android
    • Test iOS and Android with one test and helpers: using Groovy traits
    • Test using @Unroll
    • LocaleSpec Test Demo
    • CI
      • Jenkins for Android, XCodeBots for iOS
    • Sauce Labs
Java 8 for Groovy Programmers - Venkat Subramaniam
My notes
  • When you write code in Groovy, the compiler works for you. When you write code in Java, you work for the compiler ;)
  • Iteration: each in Groovy is forEach in Java 8
  • null is synonymous to word evil ;)
  • collect in Groovy is stream() in Java 8
    List<Integer> values = Arrays.asList(1,2,3,4,5)
values.stream().map(e-> e*2).forEach(System.out::println); //java 8

def values = [1,2,3,4,5]
values.collect{2 * it}.each{println it}

values.stream().filter(e->).findFirst().orElse(0)); //to avoid null when not found...groovy find returns null

  • findAll
  • inject  - Groovy
def values = [1,2,3,4]
println values.inject(0) {c, e -> c + e} //starts with 0, adds 1, takes the result adds 2 and goe on.
reduce - Java 8
values.stream().reduce(0, (c,e) -> c + e));
map gathers the data, reduce scatters the data

  • def names = [‘Jake’, ‘Sara’, ‘Pete’]
Groovy: join method of collections separated by commans
:)
Make it a law: Arrest people for writing code like for(int i=0; i<....)
Java 8:
names.stream().map(String::toUpperCase).collect(joining(“, “))); //joining is a static method from Collectors class

  • Unfamiliar things in Java 8 that are really nice (late in the party) and not there in Groovy
    • default method: List has been around for awhile. we called stream on List.
    • A method you can implement in interface is default method.
    • You can override default method, if you don’t override, it is available automatically. Abstract base classes can have state, fields. Interfaces cannot.
    • Any method in the class hierarchy takes precedence over the interface default method.
    • two interfaces with same default methods: throws error.
    • stream is implememnted using default method in Java 8
  • Java guys got promoted to Architects so that they don’t have to work any more ;)
  • Groovy interface at this time cannot have default method. You cannot create interfaces in Groovy as Groovy needs to evelve. Groovy can use Java interface with default methods.
  • Groovy has traits, give stateability.
  • Why default methods in interfaces? Without it, you will be writing utilities.
  • Nice to Use, easy to Abuse them ;)
  • Be willing to use, not to abuse ;)
  • String::toUppercase // is method reference
  • IntelliJ IDEA: There are two kinds of programmers, those who have IDEA and those who don’t have IDEA ;)
  • Method references is Venkat’s most favorite in Java 8
  • Lazy evaluation
    • Two types of methods: intermediate operation, terminal operation
    • println values.collect{
        println “collecting $it”
it * 2}.findAll {
 println “picking $it”
it > 6}.inject(0) {c, e-> c + e } //
Java 8 filter is like groovy find on collection
reduce is a terminal operation, mapand filter are intermediate operations.
Lazy in Java is Efficient ;)

Lambda expression function component in Groovy is very inefficient. In Java 8, it is extremely efficient.

  • Infinite Stream - you can create a collection that is infinite in length. Lambdas give lazy evaluation, so you can postpone.

Stream.iterate(1, e -> e + 1)
    .limit(10) //without this it runs forever ;)
    .collect(toList())); //infinite stream of 1,2,3,4….

  • Parallel collections
    • in Groovy you can use GPers to do this
    • in Java 8, stream() to parallelStream() //concurrent
    • try { Thread.sleep(1000);} catch(Exception e) {} //add delay
  • InvokeDynamic: One of the most exciting features of Java 8
    • means look the other way. Started as a way to support dynamic languages in Java 7.
    • In java 8,
    • Java introduced anonymous inner classes in 1999, when people were crying for lambdas…
    • compile a class with inner class, compile take a look at the .class….buy one, get one free ;) it’s not inner class ;)
    • It is this feature that made java 8 made performing better
    • InvokeDynamic: method binding happens at run time. It is just a binding to the method. Doesn’t incur creating the object (anonymous or what so ever), doesn’t involve object creation, so fast. Happens at the bytecode level.
    • When code is compiled, lambda expression calls become InvokeDynamic
Traits are more powerful than default methods. Groovy compiled bytecode with @CompileStatic is not as efficient as Java bytecode in Java 8.