Miles to go …

June 25, 2010

TOTD #142: GlassFish 3.1 – SSH Provisioning and Start/Stop instance/cluster on local/remote machines

Filed under: General — arungupta @ 11:23 pm

GlassFish 3.1 Milestone 2 enables SSH provisioning that allows you to create, start, stop, and delete a cluster spanning multiple instances on local and remote machines from the Domain Administration Server (DAS). This Tip Of The Day (TOTD) builds upon TOTD #141 and explains how you can create such a cluster on Amazon EC2 with Ubuntu 10.04. Carla also blogged about a similar scenario here.

The cluster topology created is shown below:

The key points shown in the topology are:

  • It consists of DAS and a remote machine "fruits" (shown in green color)
  • There is one cluster "food" spanning these two machines (shown in yellow color)
  • DAS has "broccoli" and "spinach" instances (shown in red color)
  • "fruits" has "apple", "banana", and "orange" instances (shown in red color)

Amazon EC2 assigned the public IP address of "ec2-184-72-12-163.us-west-1.compute.amazonaws.com" to DAS and "ec2-184-72-17-228.us-west-1.compute.amazonaws.com" to the remote machine. These IP addresses are used in the command invocations below.

Lets get started!

  1. Configure SSH between DAS and the remote machine – More details about SSH key setup here.
    1. Copy the keypair  generated in TOTD #141 (ec2-keypair.pem) to DAS as:
      ~/.ec2 >scp -i /Users/arungupta/.ec2/ec2-keypair.pem /Users/arungupta/.ec2/ec2-keypair.pem \\
      [email protected]
      ec2-keypair.pem 100% 1751 1.7KB/s 00:00
      

      Notice, the public IP address of DAS is specified here. This key will be used to copy the private keys generated in next step to the remote machine.

    2. Generate a private/public key pair on DAS as:
      ubuntu@ip-10-160-47-196:~$ ssh-keygen -t dsa
      Generating public/private dsa key pair.
      Enter file in which to save the key (/home/ubuntu/.ssh/id_dsa):
      Enter passphrase (empty for no passphrase):
      Enter same passphrase again:
      Your identification has been saved in /home/ubuntu/.ssh/id_dsa.
      Your public key has been saved in /home/ubuntu/.ssh/id_dsa.pub.
      The key fingerprint is:
      0a:b8:cd:8c:a0:7f:3d:00:9e:ec:ac:06:a1:f1:2f:cb ubuntu@ip-10-160-47-196
      The key's randomart image is:
      +--[ DSA 1024]----+
      | |
      | |
      | |
      |o .. |
      |o*.o. S |
      |+.=*.. . |
      |ooo.+o. |
      | ++ o o |
      |o.E+ . |
      +-----------------+
      
    3. Copy the generated public to ".ssh" directory of the remote machine as:
      ubuntu@ip-10-160-142-175:~/.ssh$ scp -i ec2-keypair.pem id_dsa.pub \\
      [email protected]:.ssh/authorized_keys2
      
    4. Make sure the ssh connection works between DAS and remote machine without specifying any key or passphrase as shown below:
      ssh [email protected]
      
  2. Install "sun-java6-jdk" and "unzip" package and GlassFish on DAS and remote machine as explained in TOTD #141. In short:
    ssh -i /Users/arungupta/.ssh/ec2-keypair.pem [email protected]
    sudo add-apt-repository "deb http://archive.canonical.com/ lucid partner"
    sudo apt-get update
    sudo apt-get install sun-java6-bin sun-java6-jre sun-java6-jdk
    sudo update-java-alternatives -s java-6-sun
    sudo apt-get install unzip
    wget http://dlc.sun.com.edgesuite.net/glassfish/3.1/promoted/glassfish-3.1-b06.zip
    unzip glassfish-3.1-b06.zip
    
  3. Start GlassFish on DAS and remote machine as:
    export ENABLE_REPLICATION=true
    export PATH=~/glassfishv3/bin:$PATH
    asadmin start-domain --verbose &
    
  4. Create the cluster and instances by issuing the following commands on the DAS
    1. Create the cluster as:
      ubuntu@ip-10-160-142-175:~$ asadmin create-cluster food
      [#|2010-06-25T22:11:27.604+0000|INFO|glassfish3.1|org.hibernate.validator.util.Version|
      _ThreadID=23;_ThreadName=http-thread-pool-4848(2);|Hibernate Validator bean-validator-3.0-JBoss-4.0.2_03|#]
      [#|2010-06-25T22:11:27.638+0000|INFO|glassfish3.1|org.hibernate.validator.engine.
      resolver.DefaultTraversableResolver|_ThreadID=23;_ThreadName=http-thread-pool-4848(2);|
      Instantiated an instance of org.hibernate.validator.engine.resolver.JPATraversableResolver.|#]
      Command create-cluster executed successfully.
      
    2. Create a node on the remote machine as:
      ubuntu@ip-10-160-142-175:~3$ asadmin create-node-ssh --nodehost \\
      ec2-184-72-17-228.us-west-1.compute.amazonaws.com --nodehome /home/ubuntu/glassfishv3 fruits
      Command create-node-ssh executed successfully.
      
    3. List all the nodes as:
      ubuntu@ip-10-160-142-175:~$ asadmin list-nodes
      localhost
      fruits
      Command list-nodes executed successfully.
      
    4. Create two instances ("broccoli" and "spinach") on DAS as:
      ubuntu@ip-10-160-142-175:~$ asadmin create-instance --cluster=food \\
      --systemproperties AJP_INSTANCE_NAME=broccoli:AJP_INSTANCE_PORT=19090 broccoli
      [#|2010-06-25T23:22:02.891+0000|INFO|glassfish3.1|javax.enterprise.system.tools.admin
      .com.sun.enterprise.v3.admin.cluster|_ThreadID=103;_ThreadName=http-thread-pool-4848(2);|
      Creating instance broccoli on localhost|#]
      [#|2010-06-25T23:22:03.750+0000|INFO|glassfish3.1|null|_ThreadID=30;_ThreadName=stdout;|
      Using DAS host localhost and port 4848 from existing das.properties for nodeagent
      ip-10-160-142-175. To use a different DAS, create a new nodeagent by specifying a
      new --nodeagent name with the correct values for --host and --port.|#]
      [#|2010-06-25T23:22:03.785+0000|INFO|glassfish3.1|null|_ThreadID=30;_ThreadName=stdout;|
      Command _create-instance-filesystem executed successfully.|#]
      Command create-instance executed successfully.
      ubuntu@ip-10-160-142-175:~$ asadmin create-instance --cluster=food \\
      --systemproperties AJP_INSTANCE_NAME=spinach:AJP_INSTANCE_PORT=19091 spinach
      [#|2010-06-25T23:22:24.813+0000|INFO|glassfish3.1|javax.enterprise.system.tools.admin.
      com.sun.enterprise.v3.admin.cluster|_ThreadID=106;_ThreadName=http-thread-pool-4848(5);|
      Creating instance spinach on localhost|#]
      [#|2010-06-25T23:22:25.636+0000|INFO|glassfish3.1|null|_ThreadID=32;_ThreadName=stdout;|
      Using DAS host localhost and port 4848 from existing das.properties for nodeagent
      ip-10-160-142-175. To use a different DAS, create a new nodeagent by specifying a
      new --nodeagent name with the correct values for --host and --port.|#]
      [#|2010-06-25T23:22:25.672+0000|INFO|glassfish3.1|null|_ThreadID=32;_ThreadName=stdout;|
      Command _create-instance-filesystem executed successfully.|#]
      Command create-instance executed successfully.
      

      The AJP_INSTANCE_NAME and AJP_INSTANCE_PORT properties will be used by mod_jk in a subsequent blog.

    5. Create three instances ("apple", "banana", and "orange") on the remote machine as:
      ubuntu@ip-10-160-142-175:~$ asadmin create-instance --cluster=food --node=fruits \\
      --systemproperties AJP_INSTANCE_NAME=apple:AJP_INSTANCE_PORT=19090 apple
      [#|2010-06-25T23:23:33.208+0000|INFO|glassfish3.1|javax.enterprise.system.tools.admin.
      com.sun.enterprise.v3.admin.cluster|_ThreadID=104;_ThreadName=http-thread-pool-4848(3);|
      Creating instance apple on fruits|#]
      [#|2010-06-25T23:23:35.682+0000|INFO|glassfish3.1|javax.enterprise.system.tools.admin.
      com.sun.enterprise.v3.admin.cluster|_ThreadID=104;_ThreadName=http-thread-pool-4848(3);|
      Command _create-instance-filesystem executed successfully.
      |#]
      Command create-instance executed successfully.
      ubuntu@ip-10-160-142-175:~$ asadmin create-instance --cluster=food --node=fruits \\
      --systemproperties AJP_INSTANCE_NAME=banana:AJP_INSTANCE_PORT=19091 banana
      [#|2010-06-25T23:23:59.697+0000|INFO|glassfish3.1|javax.enterprise.system.tools.admin.
      com.sun.enterprise.v3.admin.cluster|_ThreadID=102;_ThreadName=http-thread-pool-4848(1);|
      Creating instance banana on fruits|#]
      [#|2010-06-25T23:24:01.500+0000|INFO|glassfish3.1|javax.enterprise.system.tools.admin.
      com.sun.enterprise.v3.admin.cluster|_ThreadID=102;_ThreadName=http-thread-pool-4848(1);|
      Using DAS host ip-10-160-142-175.us-west-1.compute.internal and port 4848 from
      existing das.properties for nodeagent ip-10-160-142-20. To use a different DAS,
      create a new nodeagent by specifying a new --nodeagent name with the correct
      values for --host and --port.
      Command _create-instance-filesystem executed successfully.
      |#]
      Command create-instance executed successfully.
      ubuntu@ip-10-160-142-175:~$ asadmin create-instance --cluster=food --node=fruits \\
      --systemproperties AJP_INSTANCE_NAME=orange:AJP_INSTANCE_PORT=19092 orange
      [#|2010-06-25T23:24:13.286+0000|INFO|glassfish3.1|javax.enterprise.system.tools.admin.
      com.sun.enterprise.v3.admin.cluster|_ThreadID=105;_ThreadName=http-thread-pool-4848(4);|
      Creating instance orange on fruits|#]
      [#|2010-06-25T23:24:15.089+0000|INFO|glassfish3.1|javax.enterprise.system.tools.admin.
      com.sun.enterprise.v3.admin.cluster|_ThreadID=105;_ThreadName=http-thread-pool-4848(4);|
      Using DAS host ip-10-160-142-175.us-west-1.compute.internal and port 4848 from
      existing das.properties for nodeagent ip-10-160-142-20. To use a different DAS,
      create a new nodeagent by specifying a new --nodeagent name with the correct
      values for --host and --port.
      Command _create-instance-filesystem executed successfully.
      |#]
      Command create-instance executed successfully.
      
  5. Start the cluster
    1. List all instances as:
      ubuntu@ip-10-160-142-175:~3$ asadmin list-instances
      broccoli not running
      spinach not running
      apple not running
      banana not running
      orange not running
      Command list-instances executed successfully.
      
    2. Start the cluster as:
      ubuntu@ip-10-160-142-175:~$ asadmin start-cluster food
      . . .
      Command start-cluster executed successfully.
      
      
    3. List all the instances again as:
      ubuntu@ip-10-160-142-175:~$ asadmin list-instances
      . . .
      broccoli running
      spinach running
      apple running
      banana running
      orange running
      

      The HTTP ports of each instance can be grepped from DAS’s "domain.xml". Here are the ports for each created instance:

      broccoli 28080
      spinach 28081
      apple 28080
      banana 28081
      orange 28082

      On Amazon, you may have to poke holes in the firewall as:

      ec2-authorize default -p 28080
      ec2-authorize default -p 28081
      ec2-authorize default -p 28082
      

      And now "http://ec2-184-72-12-163.us-west-1.compute.amazonaws.com:28080/" ("broccoli" instance on DAS) will show the default index page. Similarly other host and port combinations will show this page as well.

This blog showed how to create a GlassFish 3.1 cluster spanning multiple instances on Amazon EC2 with Ubuntu 10.04.

Subsequent blogs will show:

  • How to deploy an app to this cluster and some variations ?
  • How to front-end this cluster with mod_jk for load-balancing ?

Technorati: totd glassfish clustering ssh instance amazon ec2 ubuntu

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot

June 24, 2010

TOTD #141: Running GlassFish 3.1 on Ubuntu 10.04 AMI on Amazon EC2

Filed under: General — arungupta @ 11:48 am

GlassFish 3.1 Milestone 2 was released this week, download the zip file.

TOTD #138 provide simple instructions to get you started with Milestone 1 and you can follow similar steps to get started with Milestone 2 as well. A more detailed blog on the new functionality (SSH Provisioning and Dynamic App Deployment) is coming as well.

In order to test the newly added clustering capabilities of GlassFish 3.1, I decided to run Milestone 2 build on a Ubuntu 10.04 instance on Amazon EC2. My host operating system is Mac OS X and even though the steps are defined at multiple locations (mentioned below) but complete set of steps were missing. This Tip Of The Day (TOTD) shows how get started with running a GlassFish 3.1 build on Ubuntu 10.04 instance on Amazon EC2.

This TOTD consulted the following blogs:

  • Starting Amzon EC2 with Mac OS X
  • EC2 Starters Guide on Ubuntu
  • Ubuntu 10.04 LTS Daily Build – Amazon Published EC2 AMIs
  • Ubuntu Linux Install Sun JDK and JRE
  • How to install Sun Java on Ubuntu 10.04 LTS

Twitterverse was very helpful and Divyen helped smoothen the rough edges!

Lets get started!

  1. In your home directory, create a new ".ec2" directory.
  2. Generate SSH Key & X.509 certificates
    1. Generate X.509 certificates key
      1. Go to your AWS account and click on "Security Credentials".
      2. In "Access Credentials" section, click on "X.509 Certificates" and click on "Create a new Certificate". Make sure to download the private key file and X.509 certificate in ".ec2" directory as "cert-xxxxxx.pem" and "pk-xxxxxxx.pem".
    2. SSH key – Public AMIs can be accessed using an ssh key. Give the following command in ".ec2" directory:
      ec2-add-keypair ec2-keypair > ec2-keypair.pem
      

      This will generate the SSH key in the file "ec2-keypair.pem".

  3. EC2 API Tools
    1. Download Amazon EC2 API Tools from here and unzip them in ".ec2" directory.
    2. Move "lib" and "bin" directory from the extracted directory to ".ec2" directory.
  4. Create ".ec2.profile" file in ".ec2" directory and add the contents:
    export EC2_HOME=~/.ec2
    export PATH=$PATH:$EC2_HOME/bin
    export EC2_PRIVATE_KEY=`ls $EC2_HOME/pk-*.pem`
    export EC2_CERT=`ls $EC2_HOME/cert-*.pem`
    export EC2_URL=https://ec2.us-west-1.amazonaws.com
    

    The last line sets the default zone to US-West-1. The AMI id used later is from this region. Source this file by giving the following command in ".ec2" directory:

    source ".ec2.profile"
    
  5. On your firewall, authorize port 22 for SSH and 8080 for HTTP access for GlassFish on your firewall as:
    ec2-authorize default -p 22
    ec2-authorize default -p 8080
    
  6. Run the instance as:
    ec2-run-instances ami-c597c680 -k ec2-keypair
    
  7. Obtain the public IP address of Ubuntu instance as:
    ec2-describe-instances
    RESERVATION     r-XXXXXXXX      XXXXXXXXXXXX    default
    INSTANCE        i-XXXXXXXX      ami-c597c680    ec2-XX-XX-XX-XX.us-west-1.compute.amazonaws.com       ip-XX-XX-XX-XX.us-west-1.compute.internal       running ec2-keypair     0               m1.small        2010-06-24T16:23:44+0000        us-west-1a      aki-XXXXXXXX    monitoring-disabled      XX-XX-XX-XX   XX-XX-XX-XX
    

    The address "ec2-XX-XX-XX-XX.us-west-1.compute.amazonws.com" is the public IP address and will be used for ssh next.

  8. SSH to the ready Ubuntu instance as:
    ssh -i ec2-keypair.pem [email protected]
    

  9. Install JDK 6 on Ubuntu
    1. Add the Ubuntu partner repository as:
      sudo add-apt-repository "deb http://archive.canonical.com/ lucid partner"
      
    2. Update the list of packages as:
      sudo apt-get update
      
    3. Install JDK 6 as:
      sudo apt-get install sun-java6-bin sun-java6-jre sun-java6-jdk
      
    4. Make sure the recently added JDK is at the top of JVM search order by giving the command:
      sudo update-java-alternatives -s java-6-sun
      

      This command adds "/usr/lib/jvm/java-6-sun" to the top of "/etc/jvm" file.

  10. Install "unzip" package as:
    sudo apt-get install unzip
    
  11. Download & start GlassFish 3.1
    1. Download GlassFish 3.1 Milestone 2 as:
      wget http://dlc.sun.com.edgesuite.net/glassfish/3.1/promoted/glassfish-3.1-b06.zip
      
    2. Unzip the downloaded zip file as:
      unzip glassfish-3.1-b06.zip
      
    3. Start GlassFish as:
      ./glassfishv3/glassfish/bin/asadmin start-domain --verbose
      
    4. And now your default web page is accessible at "http://ec2-XX-XX-XX-XX.us-west-1.compute.amazonws.com:8080".
  12. Finally terminate the instance as:
    ec2-terminate-instances  i-XXXXXXXX
    

How are you using GlassFish in the cloud ?

Technorati: totd glassfish ubuntu amazon ec2 cloud osxtips

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot

June 23, 2010

QA#2: Java EE 6: Integrated specifications, simple and annotation driven, next level of industry standard – by Markus Eisele

Filed under: General — arungupta @ 10:45 am

This blog entry is second (previous ones) in the Java EE 6 feedback from the community series. You can learn about how Java EE 6 is currently being used in production, development and deployment environments used within the community, and even feature requests for Java EE 7.

This entry comes from Markus Eisele – a senior technology consultant working for msg systems ag in Germany. The msg systems ag is one of the top 10 IT consulting and systems integration companies in Germany. Markus works as a software architect, developer and consultant. He also writes for German IT magazines. Markus joined msg in 2000 and has been a member of the
Center of Competence IT-Architecture since then. After being a Technical Director with BEA Systems, he has been a proud member of the Oracle ACE Directors program since 2008 (for Fusion Middleware and SOA). He works daily with customers and projects dealing with Enterprise Java and other new technologies, on a variety of platforms using different vendors. You can catch him on @myfear or read his blog.

Here is a short summary of Java EE 6 from him:

Higher integrated specifications, simple and annotation driven, single-classloader WARs, next level of industry standard

Read on for other fun stuff …

1. How are you using Java EE 6 today ? What limits your adoption ?

Enterprise Java has been my business since more than 9 years now. The first J2EE server I was using was the former BEA WebLogic 5.1. It supported Java 2 Platform, Enterprise Edition Specification, v. 1.2 (containing EJB 1.1, JDBC 2.0, JSP 1.1, Servlet 2.2). To be honest, it wasn’t big fun looking back into these days. There was lots of configuration and weird coding. And don’t even think about full-blown IDE support. It was very basic and lots of coding had to be done without any assistance.

Compared to that Java EE 6 seems to be this feels as if it is light years ahead. So many things happened to the standard in the past and it is still very exiting to see the ongoing changes. Even though I am not a member of any related expert group, I am closely monitoring the public discussions and outcomes. What I can evaluate more is the adoption in the enterprise with our customers. They are more like fast followers than early adopters. And that’s basically the setup I am looking at Java EE 6 today. More from an educational or research point of view. The goal is to be ready when first customers jump in. We have to answer questions for them, like:

- What can we do to migrate existing applications?
- What is the most standards compliant way of using this technology?
- What application servers should we use?
- What do the new specifications save in terms of time and/or money?
- How stable are new product versions?

If first projects start using new standards, we will be there to support them and already have answers at hand. At the moment we do not start over with Java EE 6 in projects. First projects are expected with the first commercial server versions from Oracle or IBM (WebLogic respectively WebSphere). Some of our customers are using GlassFish and they are sticking to the latest GlassFish 2.x versions while waiting for the clustering support coming in GlassFish 3.1. In the meantime we try to enable the developers with workshops and informative sessions about new features and updated specs.

2. What Java EE 6 technologies are you using and why ?

I am still working my way through the new features at the moment. Of both: GlassFish 3.x and Java EE 6. I started with the new CDI (JSR-299 + JSR-330) capabilities and looked at the improvements for implementing business logic with EJB 3.1 and JPA 2.0. The JSF 2.0 features gave a face to many of my prototypes.

The biggest improvements at the moment are the no-interface beans and the JPA 2.0 enhancements. In combination with the single-classloader model of WAR-deployment this is very handy for even the smallest applications. A situation we were forced to use Spring or other frameworks in the past. The ongoing annotation support is another feature-set I love to see. The reduced configuration effort with xml files seems handy but could bear risks for bigger applications using different frameworks and libraries. A big part of our daily work is cluster related. Therefore I am experimenting with the first 3.1 milestones at the moment.

3. What is your development and deployment environment ?

This is highly dependent on our customers. There is no single, company wide directive on what to use. The majority is using Eclipse. I personally prefer the latest Oracle Version (OEPE) but also have different plain Eclipse versions in use. Experimenting with GlassFish 3 I often use NetBeans 6.9 for demonstrations because of the very good GlassFish and Java EE 6 integration. I tend to be very close to productive environments in development. Therefore I am rarely using other databases than Oracle or DB2. I personally love to use MySQL for PoCs or demos.

In general we use lots of other environments based upon customer needs. Maven is the favorite build tool of choice at the moment. Longer running projects however still stick to ANT.

4. What previous versions of Java EE / J2EE have you used ? How has the migration to Java EE 6 benefited ?

I used J2EE since 1.2. Simple, technical migration from one product version to another was not too challenging at all in the past. Problems arose from switching vendors (Oracle/IBM) or updating specifications with major functional changes (CMP/JPA). Most challenging are customers staying with older product versions (and their bugs and needed workarounds). The pollution with workarounds and fixes never did any good in projects. Thankfully the times of custom frameworks to hide complexity seems to be up since Java EE 5. This was probably the most expensive part in many J2EE/Java EE 5 migrations in the past.

Seeing the advantages in OSGi based servers customers will hopefully be able to adopt new standards more easier in the future. Most valuable will be the new Web Profile. Most applications simply use the included specifications and could run on lighter environments. With the new CDI specifications we could remove some third party solutions and put it in the standard way. The so far missing JAX-RS closes a big gap left open by Java EE 5.

5. Describe the benefits of Java EE 6 to you in 120 characters.

Higher integrated specifications, simple and annotation driven, single-classloader WARs. Next level of industry standard.

6. Advice for anybody who is looking at Java EE 6 for their next project ?

If you are asking for available servers and the question, which specification is right for me today, I already tried to answering this in my blogpost http://blog.eisele.net/2010/05/java-ee-5-or-6-which-to-choose-today.html.

The programming approach itself is a advancement of existing Java EE 5 projects. Everything is pointing into the direction of POJOs and more Java SE like programming. Container services should be injected respectively annotated in a last step; if needed. Programming is getting more closer to solving business problems and not to the fulfillment of specification or container requirements.

7. What new features you’d like to see in Java EE 7 ?

/>I think the specifications are more or less feature complete. Of course I can think of a lot of improvements and I believe it’s important to stay up to date and integrate new approaches.

If you look at JSF and the RI for example, I am still missing a lot of more complex components. I always find myself implementing one or two own in my projects. If you work for  more restrictive enterprises, you are not always able to use the component suite of choice. If possible I still prefer to work with either RichFaces or PrimeFaces. Further on the cooperation between JSR 299 and JSR 330 should be clearer in general. The interaction between all JSRs should be taken to a next level. Clear but abstract guidelines
have to be specified, who is working with whom and how. If you look at the @ManagedBean annotation for example, you have three different ways of defining beans that are managed (JSR-314, JSR-316, JSR-299). That’s not exactly what I would call intuitive ;)

Generally I would love to see the Managed Beans specification becoming the heart of enterprise java. Containers should belong to the past. Services should be available to POJOs and injected (and of course started) as needed. This could speed up the whole environment a lot and frees developers from having to implement interfaces. But the most important improvements would be administrative. Java EE 7 should put those pruning ideas forward and realy try to drop some old stuff. And finally it is time to
solve all those never-ending licensing discussions about the TCKs!

Are you using, consulting, training, authoring books, etc in Java EE 6 ? Drop a comment on this blog and I’ll line you up for the Q&A session :-)

The Java EE 6 hub is your key resource to learn all about the technology.

And you can always try all Java EE 6 features in GlassFish.

Technorati: javaee6 community feedback markuseisele glassfish v3

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot

June 22, 2010

ClustrMaps Archive – Jun 21, 2010

Filed under: General — arungupta @ 5:10 am

The clustrmaps on this blog are scheduled to be archived sometime around today. And so here is a snapshot of visitors to this blog from Jun 21, 2009 to Jun 22, 2010:

The clustrmap reports a total of 267,931 visits from all over the world. The 2009 archive was certainly much richer but I’ve been tweeting more often since then!

And here is the same map with smaller clusters:

Technorati: blogs bsc milestogo clustrmaps

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot

June 21, 2010

Uber Conf 2010 – Day 4 Report – OSGi/Java EE in GlassFish and Getting Started with Clojure

Filed under: General — arungupta @ 10:15 am

Uber Conf Day 4 (Day 3, 2, and 1) started with yet another 10 miler with fellow attendees in 1 hr 18 mins and 7:49 pace. It was slightly slower than the day before – guess physical exhaustion was setting in ;-) Here are the mile splits:

Thanks to Andy for showing how to turn on the Auto-Lap feature in Garmin 305 … really handy!

I gave a talk on how GlassFish provides a great execution environment for deploying OSGi, Java EE, and hybrid applications easily. The slides are given below:

OSGi & Java EE in GlassFish – Best of both worlds

The remainder of the day was spent in Clojure track by Stuart Halloway. Next are some basic notes for getting started with Clojure:

Why Clojure ?

  • Power (hosted on jvm)
  • Robustness (functional)
  • Concurrency (identity, state, time)
  • Abstraction (OO done right)
  • Focus (lisp)

Together, these features add up to simplicity. Simplicity means

  • absence of incidental complexity
  • sufficient to meet your design constraints

Simplicity does not mean

  • Familiarity has nothing to do with simplicity
  • its not superficial, goes to the bone (e.g. only simpler syntax is just a sugar wrapper, look for the supported idioms)
  • does not mean easy, its very hard to be simple

Lot of code in Clojure is not about "set" something, its about invoke a function on it.

How to get started ?

Download the latest stable release or cutting edge build. The Clojure REPL (Read-Evaluate-Print Loop) is the easiest way to run a Clojure program as shown below:

~/tools/clojure/clojure-1.1.0 >java -cp clojure.jar clojure.main
Clojure 1.1.0
user=> (print "Hello World")
Hello Worldnil
user=> ^D

OR

~/tools/clojure/jun7 >java -cp clojure.jar clojure.main
Clojure 1.2.0-master-SNAPSHOT
user=> (print "Hello World")
Hello Worldnil
user=> ^D

Notice, the first fragment shows how to run REPL using the latest stable release and the second fragment shows the same using the cutting edge build. "Ctrl+D" exits the REPL shell. Stuart mentioned that the Clojure community stays on the cutting edge build most of the times.

Alternatively, you can also clone "labrepl" from "http://github.com/relevance/labrepl" which is an environment for exploring the Clojure language. It also provides complete instructions for getting started with NetBeans/Enclojure, Eclipse/Counterclockwise, Maven, Max/Linux command line, Windows command line, IDEA/La Clojure, and Emacs.

Configuring Clojure in NetBeans using the Enclojure plugin with NetBeans 6.9 gives the following error:

but works easily with NetBeans 6.8. The IDE seem to support REPL, Java/Clojure integration, syntax highlighting, brace/paren/bracket matching, code navigation, debugger and lots of interesting features.

Here is a typical REPL getting started session with NetBeans and Enclojure:

Here are some equivalent syntax with Java:

Semantic Java Clojure
new new Widget("foo") (Widget. "foo")
Access static members Math.PI Math/PI
Access instance members rnd.nextInt() (.nextInt rnd)
Chaining access person.getAddress().getZipCode() (.. person getAddress getZipCode)
  • "defn" is a symbol so no new syntax for adding a method.
  • ^ introduces metadata for the next symbol, "greet" in this case. You can put metadata anywhere.
  • "clojure.core" is the core of Clojure’s implementation
  • "javadoc" knows the default JDK javadocs, can make a local copy and/or extend it
  • "show" is Java reflection
  • "dir", "show", "source" are promoted from community version to the bleeding edge build.
  • Source is not always exposed
  • [1 2 3] Call the function 1 with arguments 2 & 3.
  • ‘(1 2 3) don’t evaluate it and print as is, just tick it.
  • Idiomatically Clojure requires less parentheses than Java
  • Every single function in Clojure is Runnable and Callable

  • Java interop is simple, wrapper-free (raw), performant, complete

Compojure is a small, open source Web framework for Clojure based upon Ring (similar to Python’s WSGI and Ruby’s Rack). Otherwise Clojure apps are deployed as a JAR file.

A subsequent blog will show how to deploy a simple Clojure JAR file and a simple web app using Compojure on GlassFish.

And finally, here are my top-10 reasons (in no particular order) that I’d like to attend UberConf next year:

  1. Long 90 minute sessions
  2. Lots of hands-on workshops
  3. Fun Run as part of the formal agenda
  4. Beautiful location (Westin Westminster) with lots of running trails
  5. Great food (breakfast + lunch + dinner)
  6. Small attendance so intimate discussions with speakers
  7. Great set of speakers
  8. Rarely see session hopping
  9. Pure technology, no product pitching
  10. Swags are nice – bracelet-style USB stick with all the presos, 2 different tee-shirts, fancy folder and some additional goodies for speakers

See ya next year, hopefully!

Check out the complete set of pictures at:

Technorati: conf uberconf denver clojure compojure glassfish netbeans javaee osgi

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot

June 18, 2010

QA#1 – Java EE 6: Standards compliance, Vendor independence, Light-weight deployment – by Adam Bien

Filed under: General — arungupta @ 10:47 am

The Java EE 6 platform was released over 6 months ago – Dec 10, 2009 to be specific. This blog has published multiple blog entries highlighting the agility, light-weight, ease-of-development, extensibility, and other aspects of the platform. This blog entry starts a new series sharing feedback gathered from the key Java EE 6 community members in form of Q&A. You can learn about how Java EE 6 is currently being used in production, development and deployment environments used within the community, and even feature requests for Java EE 7.

The first one comes from consultant and author Adam Bien (blog.adam-bien.com) who is an Expert Group member for the Java EE 6, EJB 3.1, and JPA 2.0 JSRs. He has worked with Java technology since JDK 1.0 and Servlets/EJB 1.0 in several large-scale projects and is now an architect and developer in Java SE/EE/FX projects. He has edited several books about Java (FX) and J2EE / Java EE and is the author of Real World J2EE Patterns (http://press.adam-bien.com). Adam is a Java Champion and JavaOne 2009 Rock Star.

Here is a short summary of Java EE 6 from him:

Standards compliance, vendor independence, milliseconds and kilobyte deployment

Read on for other fun stuff …

1. How are you using Java EE 6 today ? What limits your adoption ?

For server-side development Java EE 6 is the simplest thing you can get today. Because I’m lazy – Java EE 6 is the obvious choice. You can get everything you need with a single download. It works really well. Recently I let 20 students in a workshop install IDE, application server + database …in 15 minutes. After that we started hacking. The machines were really heterogenous (Windows, Mac, Linux)- and it worked.

I’m using Java SE and EE since their inception. I didn’t used EJB 1.0 (it was before J2EE), but started with 1.1. In several projects I had to use J2EE but omitted successfully the XML-hell with XDoclet.

This was fixed with Java EE 5 – since then (>4years) there are no more workarounds necessary and you can fully concentrate on the business logic. Java EE 6 became even better.

The adoption of Java EE is far better, than expected (by me). It turns out, that in economic slow down management is really interested in standards again. This is the only explanation I have. Java EE 6 seems to take off. I had to limit the number of registrations in last workshops to 20 and repeat a workshop at a JAX-conference (one of the biggest European conferences). The organizer ask me, whether I would like to repeat a workshop, because they were already 75 registrations. The workshop was repeated – …with about 160-180 attendees.

J2EE was said to be too complex for simple applications. The opposite is true in Java EE 6. It turns out to be any leaner, simpler and more maintainable comparing it with the alternatives. All attempts of wheel re-invention I saw so far resulted in far more complex and harder to understand code.

2. What Java EE 6 technologies are you using and why ?

I’m really enjoying the single-classloader model of WAR-deployment. In all new Java EE 6 projects we got rid off “modular” EARs and EJB-JARs and deployed everything into a WAR. It doesn’t seem to be hype-compatible, but it works very well. Especially if you don’t have any additional versioning or modularization requirements. The lack of dynamic reloading and module-hot-replacement in product is given for the majority of all enterprise projects. In real world the deployment units become even coarser.

I start the implementation of the business logic with EJB 3.1 and JPA 2.0. In case remoting is necessary – I use REST (JAR-RS) with JSON for the vast majority of all cases. CDI (JSR-299 + JSR-330) is even more powerful, but I don’t use the extended dependency injection capabilities (produces, alternatives, custom annotations, stereotypes, events) from the beginning, rather than applying them selectively to solve particular problems. CDI allows you to realize a factory, observer or strategy patterns without any overhead. In fact a plain Java SE solution would be even more bloated.

For the web part – I start with JSF 2.0. Also the adoption of JSF 2.0 is unique. There are many books, tutorials, frameworks and libraries available already. I attended a JSF conference this year with several hundred attendees.

Frameworks like Wicket or GWT are very interesting. I always start, however, with the evaluation of JSF 2. Is is already available on every Java EE 6 server – so why don’t use it. 

I would choose the alternative web frameworks, in case there is an evidence that the solution would be leaner, cheaper or easier to maintain. In the majority of all recent cases – JSF 2.0 was just good enough. Before JSF 2.0 I often used Wicket.

3. What is your development and deployment environment ?

I’m using mainly NetBeans 6.9 “Java Edition” with Glassfish v3 and Java DB / Derby (in early iterations). After the setup and stabilization phase I always use the production databases. These are mainly Oracle, sometimes DB 2 or MySQL.

In smaller teams I prefer the NetBeans build-system (Ant). In bigger teams and projects Maven. NetBeans supports both flavors really well, but Ant incremental deployment is orders of magnitudes faster than maven. You can redeploy an EJB/CDI or JPA entity with Ant in millisecods, with Maven it will take seconds. This is a huge productivity gain.

I also have an IntelliJ 9.0.2 Ultimate license and use this IDE as well. It excels NetBeans in some areas like e.g. JPA QL autocompletion. These features, however, are part of the commercial distribution and so hard to introduce into teams. In most cases I fallback to NetBeans.

4. What previous versions of Java EE / J2EE have you used ? How has the migration to Java EE 6 benefited ?

I used J2EE since 1999. The migrations were never fun. Except the migrations from J2EE into Java EE 5. Because Java EE 5/6 have still to support the old J2EE components, you can deploy your old application into a new server and start to delete code. The benefits are obvious: you can delete > 60% of the code without any impact on the functionality. With the code you can also get rid off strange patterns, extensive layering and other strange “legacy” practices. This leads to reduction of the maintenance costs.

5. Describe the benefits of Java EE 6 to you in 120 characters.

Standards compliance, vendor independence, milliseconds and kilobyte deployment.

6. Advice for anybody who is looking at Java EE 6 for their next project ?

Reduce to the max. Concentrate on the business logic and not the technology. Write plain Java SE domain objects first (Unit-Tests of course before that :-) ), introduce EJB 3.1 / CDI facades later. Don’t even try to use everything what is offered by Java EE 6. But before you start to implement you own solution, look at Java SE 6 first, then Java EE 6. You will be surprised how much functionality is already there.

7. What new features you’d like to see in Java EE 7 ?

Further simplification and unification of the APIs would be great. The synergies of JSF 2, EJB and CDI could be a lot more leveraged. A more natural use of java.util.concurrent in Java EE would be also interesting (e.g. injection of Executors). For all of my cases @Asynchronous worked perfectly, but Executors seem somehow to fascinate developers :-)

JPA 2.0 could
be also extended with some useful features like support of resource injection (EntityManager is the most important) into JPA-entities. Its a controversial topic – but really important for projects. Also better support for stateful environments would be nice.

I would also expect a standardized JAX-RS client. Something like jersey, but with javax.ws.rs. prefix :-)

I’m really surprised, that actually no-one really complains over the really old JMS programming model (1997). This spec could be drastically simplified. A plain injection of a simplified queue or topic would be sufficient for > 80% of all cases.

Are you using, consulting, training, authoring books, etc in Java EE 6 ? Drop a comment on this blog and I’ll line you up for the Q&A session :-)

The Java EE 6 hub is your key resource to learn all about the technology.

And you can always try all Java EE 6 features in GlassFish.

Technorati: javaee6 community feedback adambien glassfish v3

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot

June 16, 2010

Uber Conf 2010 – Day 3 Report

Filed under: General — arungupta @ 11:00 pm

Day 3 of Uber Conf 2010 (Day 2 and 1), again, started with a great run with fellow attendees – 10 miles in 1:15:22 with an average pace of 7:28 minutes/mile. Here is the speed-vs-elevation chart:

And complete details about the run are available below:

After a mentally and physically exhausting day, lets see how many runners show up for the run tomorrow morning. I’ll try to be outside the lobby by around 6:30 am-ish.

Attended a 3 hour Scala for Java Programmers Workshop by Venkat and extensive notes + code samples on that are available here.

The next interesting talk I attended was on Emergent Design by Neal Ford and here are the key points:

  • Emergent Design is about finding idiomatic patterns (technical and domain) in your code that already exists
  • Architecture is stuff that’s hard to change later – e.g. Language, Web framework. You write code on top of that framework and is the Design element which can be easily changed.
  • What is software design ? More details on developerdotstar.com/mag/articles/reeves_design.html
  • There are known unknowns and there are unknown unknowns – just like software.
  • Things that obscure emergent design: nature of complexity (essential and accidental),technical debt,rampant genericness
  • Emergent design enablers – TDD, Refactoring to remove technical debt, harvesting/cultivating idiomatic patterns
  • Use testing as a design tool, design will emerge from tests, will have better abstractions and less complexity
  • Reduce your Technical Debt by writing simpler code

I attended part of the Hudson Workshop and caught some part of Pragmatic Architecture Workshop.

Ted mentioned in his talk about three categories of knowledge:

  1. Things you know
  2. Things you know you don’t know
  3. Things you don’t know what you don’t know

The goal is to move maximum amount of things from Category #3 to #2 and then from #2 to #1.

Attended another talk on How to Approach Refactoring by Venkat and here are the key points captured:

  • "Refactoring: genuine desire to improve the quality of your code and design in it" and it keeps you agile
  • Write a little code, make it better, write a little code, make it better, … – evolutionary or emergent design leading to agile
  • Hardest part in refactoring & unit testing: slow down and take small steps
  • Commenting the code during refactoring is like leaving a dead rat on the table, that’s what SCM is for
  • We fear to fail, the more we fail in small steps the more we succeed in bigger steps – go refactor your code and make it agile
  • Simplicity, Clarity, Brevity, Humanity – best principles to design and refactor code
  • Integrate more often and its less time consuming; Integrate less often and its more time consuming
  • Principles
    • Check in before you start refactoring
    • Rely on automated tests
    • Reduce, not add, code when refactoring
    • Follow the KISS (Keep It Simple Stupid)
    • Keep it DRY
    • Make code self documented, let the test be the documentation
    • Write readable code
    • Check in frequently, take small steps
    • Keep code at one level of abstraction

The evening concluded with a nice party and a wonderful magic show by Michael Carducci. Enjoy a short video of Jay "trying" to perform a magic trick:


Here are some pictures captured from earlier today:

And then the evolving album:

Technorati: conf uberconf denver refactoring design software architecture

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot

Uber Conf 2010 – Notes from Scala for Java Programmers Workshop

Filed under: General — arungupta @ 4:27 pm

I spent the first half of Day 3 of Uber Conf 2010 (Day 2 and 1) in Scala for Java Programmers workshop by Venkat, took extensive notes, and noted/tried pretty much all the code samples.

<warning>
This is a code intensive and slightly long entry.
</warning>

A more complete entry covering other events of Day 3 will come later today.

Lets go down the rabbit hole!

  • Why Scala ? – Scala is highly concise, expressive, nice support for XML processing, pattern matching, inherent support for authoring concurrent code – lot of good things to write efficient code.
  • Scala means SCAlable LAnguage – comes from the fact that you can write either a small script or a complete application using Scala.
  • An application developed on single core and deployed on multi-core can easily misbehave, primarily because of the concurrency model in Java. Scala makes it much easier to write concurrent applications (more on this below).
  • Writing correct synchronized code in Java requires divine power. Multi-threaded programming in Java is like working with mother-in-law, basically waiting for you to fail.
  • Scala complies with Java semantics without you worrying about it. "-savedcompile" saves the generated class files as JAR in the local directory.

Here is the first sample code that calculates the factorial of a number:

def fact(number: Int) = {
var total = 1
for (i <- 1 to number) {
total = total * i
}
total
}
println(fact(5))

Key points about this code (differences from Java):

  • Each class is "public" by default
  • No "main" method
  • "return" keyword is not required
  • The index "i" is assigned a type using inference, integer in this case
  • "var" defines a variable

This was done as a user exercise and so I wrote my first Scala code today … yeee-haw!

  • Recursion is very expressive but expensive because of multiple stacks. Scala makes it run iterative and so less expensive
  • Scala supports tail recursion to a fairly decent extent, only one level of stack and so less expensive.

Here is a re-write of the above function using simple iterative recursion:

def total(number:Int) : Int = {
if (number == 1)
return 1
number * total (number - 1)
}
println(total(5))

And now this method converted to use tail recursion

def total(number: Int) : Int = {
def totalWithAccumulator(accumulator: Int, number: Int) : Int = {
if (number == 1)
accumulator
else
totalWithAccumulator(accumulator * number, number - 1)
}
totalWithAccumulator(1, number)
}
println(total(5))

Here is a function that calculates the total of elements in a list:

def total(list: List[int]) = {
var total = 0
for (e <- list)
total = total + e
total
}
val list = List(1,2,4,5,6,3)
println(total(list))

This is using iterative approach – keep modifying the variable over & again and tells the next step. In functional, variables are constant and never changed. And here is a new method using the functional capabilities:


// imperative
def total(list: List[int]) = {
var total = 0
for (e <- list)
total = total + e
total
}
// functional
def total2(list: List[int]) = {
list.foldLeft(0) {
(carryOver, e) => carryOver + e
}
}
val list = List(1,2,4,5,6,3)
println(total(list))
println(total2(list))

In Scala

a.+(b);

is equivalent to

a + b

".", parentheses, and ";" are optional.

When the method name ends with the colon, Scala will transpose them. The following code snippet shows this and some other features of the language:

val list = List(1,2,4,5,6,3)
// Imperative
var total1 = 0
for (e <- list)
total1 = total1 + e
println("Total is " + total1)
// Functional
val total2 = list.foldLeft(0) { (carryOver, e) => carryOver + e }
println("Total is " + total2)
// Transposition using colon
// /: is a different name for foldLeft
var total3 = (0 /: list) { (carryOver, e) => carryOver + e }
println("Total is " + total3)
// No name required if the parameters are used only once
var total4 = (0 /: list) { _ + _ }
println("Total is " + total4)
// Pipe the method
def add(op1: Int, op2: Int) = { op1 + op2 }
val total5 = (0 /: list) { add }
println("Total is " + total5)
  • No primitive types in Scala, everything is an object

Code samples of closures in Scala …

def totalSelectedNumbers(list:List[Int], selector: Int => boolean) = {
var total = 0
for (e <- list)
if (selector(e)) total = total + e
total
}
val list = List(1,2,4,5,6,3)
println(totalSelectedNumbers(list, { e => true }))
println(totalSelectedNumbers(list, { e => e % 2 == 0 }))
println(totalSelectedNumbers(list, { e => e > 4 }))
println(totalSelectedNumbers(list, { _ > 4 }))

Notice how "println" becomes more concise over subsequent lines. And a further cleaner, and concise, version can be written by allowing multiple lists to be passed to the function as shown below:

// Multiple lists to the function
def totalSelectedNumbers(list:List[Int]) (selector: Int => boolean) = {
var total = 0
for (e <- list)
if (selector(e)) total = total + e
total
}
val list = List(1,2,4,5,6,3)
println(totalSelectedNumbers(list) { _ > 4 })

Here is another exercise to write a function that doubles the number in the list:

val list = List(1,2,4,5,6,3)
println(list.map { _ * 2 })

And another exercise to find the maximum number of a list

val list = List(1,2,4,5,6,3,2,7)val max = (list(0) /: list) { (max, e) =>              if (e > max) e else max           }println (max)

And then a simplified method definition …

println((list(0) /: list) { Math.max })

  • In Scala: "val" creates a pointer that cannot change, "var" creates a pointer that can change but the objects are immutable.
  • In Scala, everything is public unless you say otherwise.
  • Code within the class runs within the primary constructor. In Scala, there is only one constructor, every other is called as auxiliary constructor and they always go through the primary constructor. Only primary constructor can call super (only path to the base class)

Here is a simple class definition:

class Car(val year: int, var miles: Int) {
// invokes with primary constructor
println("hello there")
// year has a getter, miles has a getter/setter
def drive(dist: Int) = {
miles += dist
}
// private method only to this instance
private[this] def tuneup {}
}
val car = new Car(2010, 0)
println(car.year)
println(car.miles)
car drive 10
println(car.miles)
  • Scala is a purely OO language, new methods can be defined easily on any object

Here is a sample code that demonstrates adding a new method:

import java.util._ // static import
class IntUtil(number: Int) {
def days = this
def ago = {
var cal = Calendar.getInstance()
cal.add(Calendar.DAY_OF_MONTH, -number)
cal.getTime()
}
}
val num = 2
val obj = new IntUtil(num)
println(obj.days.ago)
  • Scala has no primitive types, everything is an object.
  • New methods can be easily added using "implicit conversion"

Here is a code sample that shows how to perform "implicit conversion" and add a new method to a "val"

import java.util._ // static import
class IntUtil(number: Int) {
def days = this
def ago = {
var cal = Calendar.getInstance()
cal.add(Calendar.DAY_OF_MONTH, -number)
cal.getTime()
}
}
val num = 2
val obj = new IntUtil(num)
println(obj.days.ago)
// implicit conversion - only one conversion can be applied per object (or class??)
implicit def pleaseConvertInt2WonderfulIntUtil(number: Int) = new IntUtil(number)
println(2.days.ago)
  • To create Singleton, use the word "object" such as:
class Car {
}
object Car {
// place all the static methods here
}
  • This singleton object is also called the "companion" object and it must exist in the same file as the original object.
  • Companion object is used to create the factory pattern.
  • Adding "private" to the class definition makes the constructor private.
  • "apply" is a special method: "blah.apply(something)" is equivalent to "blah(something)"
  • "apply" has a cousin "update" and takes 2 parameters

Here is a code that demonstrates some of the above mentioned features:

import scala.collection.mutable._
class Pen private (val color: String) {
println(color + " pen being created")
}
// companion object - singleton
object Pen {
val pens : Map[String, Pen] = Map()
def createPen(color: String) = {
if (!pens.contains(color))
pens(color) = new Pen(color)
// == pens.update(color, new Pen(color)
pens.get(color)
}
}
val bluePen = Pen.createPen("blue")
val bluePen2 = Pen.createPen("blue")
val redPen = Pen.createPen("red")

Running this code shows that "blue" pen is created only once and cached during second attempt. Notice, how the Pen is created using "createPen" factory method. This code can be changed to use "apply" as:

import scala.collection.mutable._
class Pen private (val color: String) {
println(color + " pen being created")
}
// companion object - singleton
object Pen {
val pens : Map[String, Pen] = Map()
def apply(color: String) = {
if (!pens.contains(color))
pens(color) = new Pen(color)
pens.get(color)
}
}
val bluePen = Pen("blue")
val bluePen2 = Pen("blue")
val redPen = Pen("red")

Notice how default constructors instead of the factory pattern are now used.

Here is a piece of code that has two classes with similar functionality:

class Human(val name: String) {
def listen = println("I am " + name + " your friend")
}
class Animal(val name: String)
class Dog(override val name: String) extends Animal(name) {
def listen = println("I am " + name + " your friend")
}
val peter = new Human("Peter")
peter.listen
val rover = new Dog("Rover")
rover.listen

This violates the DRY principle. Scala Traits allows to encapsulate the code as shown below:

trait Friend {
val name: String
def listen = println("I am " + name + " your friend")
}
class Human(val name: String) extends Friend
class Animal(val name: String)
class Dog(override val name: String) extends Animal(name) with Friend
val peter = new Human("Peter")
peter.listen
val rover = new Dog("Rover")
rover.listen
// include trait at an object level
class Cat(override val name: String) extends Animal(name)
val molly = new Cat("Molly") with Friend
molly.listen
  • XML processing is a first class citizen in Scala
val xml = therethere
println(xml)
// XPath query
println(xml \\ "@greet")
println(xml \\ "where")
val stock = Map("APPL" -> 250, "GOOG" -> 300)
def createStock() = {
stock.map { entry =>  }
}
// Scala code within XML
val xml = { createStock() }
println(xml)

Here is the last exercise from the workshop: Use Yahoo Weather API to find the temperature of Denver, CO:

import scala.xml._
import scala.io._
import java.net._
val theURL = "http://weather.yahooapis.com/forecastrss?w=2391279&u=f"
val xmlString = Source.fromURL(new URL(theURL)).mkString
//println(xmlString)    // for debugging
val xml = XML.loadString(xmlString)
val city = xml \\ "location" \\ "@city"
val state = xml \\ "location" \\ "@region"
val temperature = xml \\ "condition" \\ "@temp"
println("Temperature in " + city + ", " + state + " is " + temperature)
  • Actors enable concurrency in Scala, "receive" is used to receive the response from each Actor
  • Don’t use "receive" outside, use receive within
  • OK to share Java code within Scala only if its mutable
  • Scala has "tuples", are immutables, and really good candidate for concurrency

Here is a sample code refactoring the above code in a method, called 50 times in a loop, printed once using traditional method invocation and once using Actors. The total time taken for the two set of executions is then printed.

import scala.xml._
import scala.io._
import java.net._
import scala.actors._
import Actor._
def getTemperature(woeid: Int) = {
val theURL = "http://weather.yahooapis.com/forecastrss?w=" + woeid + "&u=f"
val xmlString = Source.fromURL(new URL(theURL)).mkString
val xml = XML.loadString(xmlString)
val city = xml \\ "location" \\ "@city"
val state = xml \\ "location" \\ "@region"
val temperature = xml \\ "condition" \\ "@temp"
(city, state, temperature)      // tuples are immutable
// good candidates for concurrency
}
val start = System.nanoTime
for (woeid <- 2391250 to 2391299) {
println(getTemperature(woeid))
}
val end = System.nanoTime
println("Time taken (without Actor): " + (end - start)/100000000.0)
// Now dispatching to multiple actors
val start2 = System.nanoTime
val caller = self               // self is a pointer to my actor
for (woeid <- 2391250 to 2391299) {
actor { caller ! getTemperature(woeid) }      // Dispatch 50 actors
}
for (woeid <- 2391250 to 2391299) {
receive {
case msg => println(msg)
}
}
val end2 = System.nanoTime
println("Time taken (with Actor): " + (end2 - start2)/100000000.0)

A sample result from running this script showed the results:

Time taken (without Actor): 171.62816
Time taken (with Actor): 29.05179

As you can see the difference is quite evident – approx 6x better performance when using Actors.

Overall, super excited after writing so much Scala code and learned something new.

Thank you Venkat for yet another great workshop!

Technorati: conf uberconf scala java workshop

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot

June 15, 2010

Über Conf Day 2 Trip Report

Filed under: General — arungupta @ 9:04 pm

Day 2 of Über Conf (Day 1) started with about 30 runners heading out for a fun run. Some of us went farther than that and certainly felt the impact of running additional distance in this "mile high city". Here is a picture of some runners:

Here are some more pictures from the run:

Complete details about the run are below:


Many thanks to Ben Ellingson for arranging this!

The runners are meeting again outside the lobby at 7am. And if you are interested in a 10K+ run then I’ll see you in the lobby at 6am.

I gave a talk on Java EE 6 & GlassFish 3 and the slides are now available:

Java EE 6 & GlassFish 3

I attended a few sessions today and here are my notes from them.

  • Transforming to Groovy – Venkat demonstrated Groovy’s agility by taking converting a Java code to Groovy and highlighting the idioms on the way. He explained multi methods, delegation, improved reuse using Closures, Execute Around method, simplified List management, conciseness, multi-line string, file read, working with XML and other similar features of the language.
  • The Art of (Java) Benchmarking – Cliff Click talked about several micro and macro benchmarks and classified them in BUSTED and PLAUSIBLE categories with appropriate reasoning. There were clear recommendations such as warmup loops, planning for variation in results, multiple runs and reporting average and standard deviation, and many others to make sure the generated results are valid.
  • Hadoop Workshop I – The workshop required to run a VMWare image so I could not perform any of the hands-on stuff. But Matthew MucCullough gave a very good explanation of MapReduce, Hadoop, and other internals.
  • Building RESTful ROA Architecture at Orbitz – Alex Antonov talked about how Orbitz evolved from a Jini/Java-based SOA to a ROA architecture. He listed the standard request methods (GET, PUT, POST, DELETE), standard response codes (200, 202, 404, …), clear resource representation, and simplicity of HTTP on "Why REST". A significant part of the talk was about Protocol Buffers – a language/platform neutral and extensible mechanism for serializing structured data.

The post-dinner panel discussion with @paulk_asert, @pragkirk, @iansrobinson , @johannarothman, @benrady, and @stuarthalloway was interesting as well.

Here are some of the Q&A from the session:

Q: What language would you recommend for beginning computer science ?
Stuart: Scheme
Kirk: Text-based environment does not excite them, try visual environment.
Paul: Most of the languages can give as many good habits as bad habits so no specific answer.
Ian: No specific language – something main stream like Java and C# and then move around.
Johanna: May be Scheme
Ben: Pure science (Lisp, C) or software engineering (Java, C#)

Q: What is the key to building resilience ?
Johanna: Broaden the specialties, expand the horizon
Ben: People who solve problems, use architect/development/testing/etc skills to solve the problems
Kirk: Always look for something that moves you away from your comfort zone
Stuart: Learning little bit about functional programming, start learning and you’ll be there

Q. What strategies do you guys use to stay current ?
Stuart: Gadgets, follow the ambience using holes in the day (dropping kids, traffic lights, etc). Thinking during running and mechanical typing afterwards.
Ian: Highlight keywords, thinking during morning/evenings
Paul: Networking, look for smart people within your organization
Kirk: May be chained to desk 4 hrs but work hours is 10 hrs because of different gadgets
Ben: Follow the people that you know, RSS reader
Johanna: Don’t keep up, pick & choose

Q. On your best project, what was the ratio of star/steady guys ?
Ben: 10:1 in a startup, the company folded
Stuart: 2:0 or 3:0 for the dream team, 5 too big
Johanna: 85 development team – all star, respect for each other, contentious discussions.
Kirk: 75 developers, 7 individuals core group drove the project. Fundamental force that made them succeed was team cohesion (been together for 2 years)
Paul: Have people in the team who can work
Ian: Diverse teams are good

There was discussion around craftsmanship and apprenticeship. Stuart talked about how "craftsmen swap" in like-minded shops can help the growth. There was also discussion around Resume-Driven-Development (RDD). Kirk and others point was that resume basically gets you to the interview, you still need to know the skill to get the job.

And just for fun, here is the panoramic view from my hotel room this morning:

It was simply breathtaking!

And here is the evolving photo album:

Technorati: conf uberconf glassfish javaee6 denver

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot

June 14, 2010

Über Conf Day 1 Trip Report

Filed under: General — arungupta @ 10:02 pm

Jay Zimmerman kick started the 200th No Fluff Just Stuff event and Day 1 of Über Conf earlier today. This event is focused on alpha-geeks, with more access to speakers, more hands-on workshops, longer hours, and attendees from 45 different states.

Cliff Click gave a great keynote talking about Challenges and Directions in Java Virtual Machines. Here are some key points from his talk:

  • Hardware, Operating Systems, and JVM are ready for concurrent processing; Tools, Languages, IDEs, etc are not.
  • Background compilation, Concurrent GC, Introspection and continuous profiling are some opportunities for multi-core (2-4) machines.
  • Application level parallelism – JDK libraries are really useful, thread pools, pipelines of pools. No support for self-tuning and auto-sizing pipeline of thread pools.
  • Data parallelism is recommended for 4-32 core CPUs – Big data, small code, small code with lots of repetition, realm of scientific computing (weather, simulation, nuclear reactor, etc)
  • For 64-100 chips – Clustering on a chip, multiple applications communicating via pipes, GC fixed mallocs, parallelism at all levels (queues, caches, DB access, processor, …)
  • Java concurrency is not right most times because of the lack of knowledge, not because of language features
  • Software Transactional Memory (STM) is replace existing locak with transaction boundaries, Step towards declarative program #1 issue while debugging other concurrent programs – "they can’t name the shared variable"
  • Other performance pitfalls – Debugging, performance, Performance Anomalies
  • Suggestions for concurrent programming – Scala, Clojure, X10, JVSTM + Data modeling

The conference started on a positive note on a personal self with complimentary upgrade to first class in the flight and yet another complimentary upgrade to high-floor room in the hotel. I forgot my Garmin 305 USB connector but found another runner who has it, so nice!

Tomorrow morning starts with a 5K run and then Java EE 6 / GlassFish 3 talk at 10:30am in Westminster 1. The interesting part is that this is the first conference, at least I’m aware of, with a 5K run as part of the formal agenda. The hotel certainly has a lots of running trails near by and I did a 4 miler in the mile-high city. Looking forward to longer distance running on Wednesday and Thursday. The piano and magician during the evening reception added a good fun to the entire mix.

Now get ready to be physically and mentally exhausted!

Here are some pictures captured from today:

And here is the evolving photo album so far:

Technorati: conf uberconf denver glassfish javaee

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot
Older Posts »

The views expressed on this blog are my own and do not necessarily reflect the views of Oracle.
Powered by WordPress