Featured FREE Whitepapers

What's New Here?


EJB 3.x : Lifecycle and Concurrency models (part 1)

Java EE component life cycle and concurrency related details might not be new for seasoned professionals, but it is something which can take time to sink in for beginners.                As far as EJBs are concerned, understanding their life cycle (and related concurrency scenarios) is extremely critical in order to ensure appropriate usage and solution design using EJBs. It’s easy to misuse them !I’ll quickly cover Stateless and Stateful beans in this post and skip Lima Beans for the time being!Stateful Session Beans – life cycle + concurrency handling Stateless beans – concurrency model only, since I had briefly covered the lifecycle in one of my previous posts.What are the distinct states in the life cycle of a Stateful Session Bean?Does Not Exist Ready PassivatedHow do the states change? What triggers them? Here is a quick tabular snap shot and a high level diagram. For more details, read on . . .Note: DNE – Does Not Exist, R – Ready, P – Passivated, SFSB – Statelful Session Bean  State Transition Triggers CallbacksDNE to R When a SFSB instance is first accessed via JNDI or DI @PostConstructR to DNE Container shuts down, client invokes method annotated with @Remove, the bean reaches idle time out threshold designated by DD or @StatefulTimeout @PreDestroyR to P The EJB container passivates idle beans and removes them from active memory based on specific algorithms @PrePassivateP to DNE The bean reaches idle time out threshold designated by DD or @StatefulTimeout Note: @PreDestroy annotated method is NOT invokedP to R When the client invokes a SFSB instance after it is passivated but has not timed out yet @PostActivate  Note: If a SFSB throws an exception during request processing, its instance is destroyed i.e. it goes to a DNE state. The @PreDestroy annotated method is not invoked in this case Now that we have some idea about the life cycle of a SFSB, let’s try to take a look at how do these beans behave under load i.e. when the application is used by multiple users at a time which translates into concurrent access of SFSB instances Stateful Session Beans: Concurrency Management Thread safety is one of the core features of EJBs. The point to be noted is that this thread safety is free of cost and does not need any concurrency related constructs to be coded in by the bean developer himself (there are a few exceptions). As far as SFSB are concerned, the EJB container ensures that only one thread can access a bean instance at a particular time. In this example, we are trying to simulate concurrent access to a single instance of a SFSB by invoking a test Servlet via JMeter. The Servlet injects the bean via DI and calls a method on it. The SFSB method just uses a Thread.sleep() to pretend as if it’s executing something. package com.abhirockzz.wordpress.ejb.lifecycle.stateful;import java.util.Date; import java.util.logging.Level; import java.util.logging.Logger; import javax.ejb.Stateful;@Stateful public class MyStatefulBean {public MyStatefulBean() { }public void act() { System.out.println("Entered MyStatefulBean/act() on " + new Date().toString() + " . SFSB instance " + this.hashCode() + " Thread : " + Thread.currentThread().getName()); try { Thread.sleep(2000); } catch (InterruptedException ex) { Logger.getLogger(MyStatefulBean.class.getName()).log(Level.SEVERE, null, ex); }System.out.println("Exit MyStatefulBean/act() on " + new Date().toString() + " . SFSB instance " + this.hashCode() + " Thread : " + Thread.currentThread().getName());} } package com.abhirockzz.wordpress.ejb.lifecycle.stateful;import java.io.IOException; import java.util.Date; import javax.inject.Inject; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;@WebServlet(name = "SFSBTestServlet", urlPatterns = {"/SFSBTestServlet"}) public class SFSBTestServlet extends HttpServlet {public SFSBTestServlet() { }@Inject MyStatefulBean mySFSB;@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("Entered SFSBTestServlet/doGet() on " + new Date().toString() + " . Servlet instance " + this.hashCode() + " Thread : " + Thread.currentThread().getName()); mySFSB.act(); }}    ObservationsSince a Servlet itself is not thread safe, multiple threads will in fact enter the doGet() method A single instance of the SFSB (evident via the hashCode result) in being accessed concurrently (see the thread names in the logged statements) Only one thread will be able to access the SFSB instance though – other threads wait for their turn while the SFSB method returns. This delay is noticeable via the log statements on the console  What about Stateless Beans? These beans are inherently thread safe. Why? It is because by default, the container makes sure that each new request is served by a new instance of the bean. Remember, that a client can obtain a reference to stateless bean in 3 possible ways – DI, JNDI or via a remote interface (RMI). In all these cases, it is the container (proxy) which intercepts the call – thus, even if multiple threads are seemingly accessing the same bean instance, its actually not the same one! package com.abhirockzz.wordpress.ejb.lifecycle.stateless;import java.util.Date; import java.util.logging.Level; import java.util.logging.Logger; import javax.ejb.Stateless;@Stateless public class MyStatelesslBean {public void act() {System.out.println("Entered MyStatelesslBean/act() on " + new Date().toString() + " . SLSB instance " + this.hashCode() + " Thread : " + Thread.currentThread().getName()); try { Thread.sleep(2000); } catch (InterruptedException ex) { Logger.getLogger(MyStatelesslBean.class.getName()).log(Level.SEVERE, null, ex); }System.out.println("Exit MyStatelesslBean/act() on " + new Date().toString() + " . SLSB instance " + this.hashCode() + " Thread : " + Thread.currentThread().getName()); } } package com.abhirockzz.wordpress.ejb.lifecycle.stateless;import java.io.IOException; import java.util.Date; import javax.inject.Inject; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;@WebServlet(name = "SLSBTestServlet", urlPatterns = {"/SLSBTestServlet"}) public class SLSBTestServlet extends HttpServlet {@Inject MyStatelesslBean slsb;@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {System.out.println("Entered SLSBTestServlet/doGet() on " + new Date().toString() + " . Servlet instance " + this.hashCode() + " Thread : " + Thread.currentThread().getName());slsb.act();}}   ObservationsSince a Servlet itself is not thread safe, multiple threads will in fact enter the doGet() method Different instances of the SLSB (evident via the hashCode result) are being picked by the container to manage concurrent requests (see the thread names in the logged statements). In spite of concurrent requests, each request thread is getting serviced by a new instanceThat’s all for now! I am planning to cover Singleton Session beans in a future post. Stay tuned . . . . Thanks for reading!Reference: EJB 3.x : Lifecycle and Concurrency models (part 1) from our JCG partner Abhishek Gupta at the Object Oriented.. blog....

OSGi: the gateway into micro-services architecture

The terms “modularity” and “microservices architecture” pop up quite often these days in context of building scalable, reliable distributed systems. Java platform itself is known to be weak with regards to modularity (Java 9 is going to address that by delivering project Jigsaw), giving a chance to frameworks like OSGi and JBoss Modules to emerge. When I first heard about OSGi back in 2007, I was truly excited about all those advantages Java applications might benefit of by being built on top of it. But very quickly the frustration took place instead of excitement: no tooling support, very limited set of compatible libraries and frameworks, quite unstable and hard to troubleshoot runtime. Clearly, it was not ready to be used by average Java developer and as such, I had to put it on the shelf. With years, OSGi has matured a lot and gained a widespread community support. The curious reader may ask: what are the benefits of using modules and OSGi in particular? To name just a few problems it helps to solve:explicit (and versioned) dependency management: modules declare what they need (and optionally the version ranges) small footprint: modules are not packaged with all their dependencies easy release: modules can be developed and released independently hot redeploy: individual modules may be redeployed without affecting othersIn today’s post we are going to take a 10000 feet view on a state of the art in building modular Java applications using OSGi. Leaving aside discussions how good or bad OSGi is, we are going to build an example application consisting of following modules:data access module business services module REST services moduleApache OpenJPA 2.3.0 / JPA 2.0 for data access (unfortunately, JPA 2.1 is not yet supported by OSGi implementation of our choice), Apache CXF 3.0.1 / JAX-RS 2.0 for REST layer are two main building blocks of the application. I found Christian Schneider‘s blog, Liquid Reality, to be invaluable source of information about OSGi (as well as many other topics). In OSGi world, the modules are called bundles. Bundles manifest their dependencies (import packages) and the packages they expose (export packages) so other bundles are able to use them. Apache Maven supports this packaging model as well. The bundles are managed by OSGi runtime, or container, which in our case is going to be Apache Karaf 3.0.1 (actually, it is the single thing we need to download and unpack). Let me stop talking and better show some code. We are going to start from the top (REST) and go all the way to the bottom (data access) as it would be easier to follow. Our PeopleRestService is a typical example of JAX-RS 2.0 service implementation: package com.example.jaxrs;import java.util.Collection;import javax.ws.rs.DELETE; import javax.ws.rs.DefaultValue; import javax.ws.rs.FormParam; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.ws.rs.core.UriInfo;import com.example.data.model.Person; import com.example.services.PeopleService;@Path( "/people" ) public class PeopleRestService { private PeopleService peopleService; @Produces( { MediaType.APPLICATION_JSON } ) @GET public Collection< Person > getPeople( @QueryParam( "page") @DefaultValue( "1" ) final int page ) { return peopleService.getPeople( page, 5 ); }@Produces( { MediaType.APPLICATION_JSON } ) @Path( "/{email}" ) @GET public Person getPerson( @PathParam( "email" ) final String email ) { return peopleService.getByEmail( email ); }@Produces( { MediaType.APPLICATION_JSON } ) @POST public Response addPerson( @Context final UriInfo uriInfo, @FormParam( "email" ) final String email, @FormParam( "firstName" ) final String firstName, @FormParam( "lastName" ) final String lastName ) { peopleService.addPerson( email, firstName, lastName ); return Response.created( uriInfo .getRequestUriBuilder() .path( email ) .build() ).build(); } @Produces( { MediaType.APPLICATION_JSON } ) @Path( "/{email}" ) @PUT public Person updatePerson( @PathParam( "email" ) final String email, @FormParam( "firstName" ) final String firstName, @FormParam( "lastName" ) final String lastName ) { final Person person = peopleService.getByEmail( email ); if( firstName != null ) { person.setFirstName( firstName ); } if( lastName != null ) { person.setLastName( lastName ); }return person; } @Path( "/{email}" ) @DELETE public Response deletePerson( @PathParam( "email" ) final String email ) { peopleService.removePerson( email ); return Response.ok().build(); } public void setPeopleService( final PeopleService peopleService ) { this.peopleService = peopleService; } } As we can see, there is nothing here telling us about OSGi. The only dependency is the PeopleService which somehow should be injected into the PeopleRestService. How? Typically, OSGi applications use blueprint as the dependency injection framework, very similar to old buddy, XML based Spring configuration. It should be packaged along with application inside OSGI-INF/blueprint folder. Here is a blueprint example for our REST module, built on top of Apache CXF 3.0.1: <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs="http://cxf.apache.org/blueprint/jaxrs" xmlns:cxf="http://cxf.apache.org/blueprint/core" xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsdhttp://cxf.apache.org/blueprint/jaxwshttp://cxf.apache.org/schemas/blueprint/jaxws.xsdhttp://cxf.apache.org/blueprint/jaxrshttp://cxf.apache.org/schemas/blueprint/jaxrs.xsdhttp://cxf.apache.org/blueprint/corehttp://cxf.apache.org/schemas/blueprint/core.xsd"><cxf:bus id="bus"> <cxf:features> <cxf:logging/> </cxf:features> </cxf:bus><jaxrs:server address="/api" id="api"> <jaxrs:serviceBeans> <ref component-id="peopleRestService"/> </jaxrs:serviceBeans> <jaxrs:providers> <bean class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider" /> </jaxrs:providers> </jaxrs:server> <!-- Implementation of the rest service --> <bean id="peopleRestService" class="com.example.jaxrs.PeopleRestService"> <property name="peopleService" ref="peopleService"/> </bean> <reference id="peopleService" interface="com.example.services.PeopleService" /> </blueprint> Very small and simple: basically the configuration just states that in order for the module to work, the reference to the com.example.services.PeopleService should be provided (effectively, by OSGi container). To see how it is going to happen, let us take a look on another module which exposes services. It contains only one interface PeopleService: package com.example.services;import java.util.Collection;import com.example.data.model.Person;public interface PeopleService { Collection< Person > getPeople( int page, int pageSize ); Person getByEmail( final String email ); Person addPerson( final String email, final String firstName, final String lastName ); void removePerson( final String email ); } And also provides its implementation as PeopleServiceImpl class: package com.example.services.impl;import java.util.Collection;import org.osgi.service.log.LogService;import com.example.data.PeopleDao; import com.example.data.model.Person; import com.example.services.PeopleService;public class PeopleServiceImpl implements PeopleService { private PeopleDao peopleDao; private LogService logService; @Override public Collection< Person > getPeople( final int page, final int pageSize ) { logService.log( LogService.LOG_INFO, "Getting all people" ); return peopleDao.findAll( page, pageSize ); }@Override public Person getByEmail( final String email ) { logService.log( LogService.LOG_INFO, "Looking for a person with e-mail: " + email ); return peopleDao.find( email ); }@Override public Person addPerson( final String email, final String firstName, final String lastName ) { logService.log( LogService.LOG_INFO, "Adding new person with e-mail: " + email ); return peopleDao.save( new Person( email, firstName, lastName ) ); }@Override public void removePerson( final String email ) { logService.log( LogService.LOG_INFO, "Removing a person with e-mail: " + email ); peopleDao.delete( email ); } public void setPeopleDao( final PeopleDao peopleDao ) { this.peopleDao = peopleDao; } public void setLogService( final LogService logService ) { this.logService = logService; } } And this time again, very small and clean implementation with two injectable dependencies, org.osgi.service.log.LogService and com.example.data.PeopleDao. Its blueprint configuration, located inside OSGI-INF/blueprint folder, looks quite compact as well: <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"> <service ref="peopleService" interface="com.example.services.PeopleService" /> <bean id="peopleService" class="com.example.services.impl.PeopleServiceImpl"> <property name="peopleDao" ref="peopleDao" /> <property name="logService" ref="logService" /> </bean> <reference id="peopleDao" interface="com.example.data.PeopleDao" /> <reference id="logService" interface="org.osgi.service.log.LogService" /> </blueprint> The references to PeopleDao and LogService are expected to be provided by OSGi container at runtime. Hovewer, PeopleService implementation is exposed as service and OSGi container will be able to inject it into PeopleRestService once its bundle is being activated. The last piece of the puzzle, data access module, is a bit more complicated: it contains persistence configuration (META-INF/persistence.xml) and basically depends on JPA 2.0 capabilities of the OSGi container. The persistence.xml is quite basic: <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"> <persistence-unit name="peopleDb" transaction-type="JTA"> <jta-data-source> osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=peopleDb) </jta-data-source> <class>com.example.data.model.Person</class> <properties> <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema"/> </properties> </persistence-unit> </persistence> Similarly to the service module, there is an interface PeopleDao exposed: package com.example.data;import java.util.Collection;import com.example.data.model.Person;public interface PeopleDao { Person save( final Person person ); Person find( final String email ); Collection< Person > findAll( final int page, final int pageSize ); void delete( final String email ); } With its implementation PeopleDaoImpl: package com.example.data.impl;import java.util.Collection;import javax.persistence.EntityManager; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery;import com.example.data.PeopleDao; import com.example.data.model.Person;public class PeopleDaoImpl implements PeopleDao { private EntityManager entityManager; @Override public Person save( final Person person ) { entityManager.persist( person ); return person; } @Override public Person find( final String email ) { return entityManager.find( Person.class, email ); } public void setEntityManager( final EntityManager entityManager ) { this.entityManager = entityManager; } @Override public Collection< Person > findAll( final int page, final int pageSize ) { final CriteriaBuilder cb = entityManager.getCriteriaBuilder(); final CriteriaQuery< Person > query = cb.createQuery( Person.class ); query.from( Person.class ); return entityManager .createQuery( query ) .setFirstResult(( page - 1 ) * pageSize ) .setMaxResults( pageSize ) .getResultList(); } @Override public void delete( final String email ) { entityManager.remove( find( email ) ); } } Please notice, although we are performing data manipulations, there is no mention of transactions as well as there are no explicit calls to entity manager’s transactions API. We are going to use the declarative approach to transactions as blueprint configuration supports that (the location is unchanged, OSGI-INF/blueprint folder): <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:jpa="http://aries.apache.org/xmlns/jpa/v1.1.0" xmlns:tx="http://aries.apache.org/xmlns/transactions/v1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"> <service ref="peopleDao" interface="com.example.data.PeopleDao" /> <bean id="peopleDao" class="com.example.data.impl.PeopleDaoImpl"> <jpa:context unitname="peopleDb" property="entityManager" /> <tx:transaction method="*" value="Required"/> </bean> <bean id="dataSource" class="org.hsqldb.jdbc.JDBCDataSource"> <property name="url" value="jdbc:hsqldb:mem:peopleDb"/> </bean> <service ref="dataSource" interface="javax.sql.DataSource"> <service-properties> <entry key="osgi.jndi.service.name" value="peopleDb" /> </service-properties> </service> </blueprint> One thing to keep in mind: the application doesn’t need to create JPA 2.1‘s entity manager: the OSGi runtime is able do that and inject it everywhere it is required, driven by jpa:context declarations. Consequently, tx:transaction instructs the runtime to wrap the selected service methods inside transaction. Now, when the last service PeopleDao is exposed, we are ready to deploy our modules with Apache Karaf 3.0.1. It is quite easy to do in three steps:run the Apache Karaf 3.0.1 container bin/karaf (or bin\karaf.bat on Windows)execute following commands from the Apache Karaf 3.0.1 shell: feature:repo-add cxf 3.0.1 feature:install http cxf jpa openjpa transaction jndi jdbc install -s mvn:org.hsqldb/hsqldb/2.3.2 install -s mvn:com.fasterxml.jackson.core/jackson-core/2.4.0 install -s mvn:com.fasterxml.jackson.core/jackson-annotations/2.4.0 install -s mvn:com.fasterxml.jackson.core/jackson-databind/2.4.0 install -s mvn:com.fasterxml.jackson.jaxrs/jackson-jaxrs-base/2.4.0 install -s mvn:com.fasterxml.jackson.jaxrs/jackson-jaxrs-json-provider/2.4.0build our modules and copy them into Apache Karaf 3.0.1‘s deploy folder (while container is still running): mvn clean package cp module*/target/*jar apache-karaf-3.0.1/deploy/When you run the list command in the Apache Karaf 3.0.1 shell, you should see the list of all activated bundles (modules), similar to this one:Where module-service, module-jax-rs and module-data correspond to the ones we are being developed. By default, all our Apache CXF 3.0.1 services will be available at base URL http://:8181/cxf/api/. It is easy to check by executing cxf:list-endpoints -f command in the Apache Karaf 3.0.1 shell.Let us make sure our REST layer works as expected by sending couple of HTTP requests. Let us create new person: curl http://localhost:8181/cxf/api/people -iX POST -d "firstName=Tom&lastName=Knocker&email=a@b.com"HTTP/1.1 201 Created Content-Length: 0 Date: Sat, 09 Aug 2014 15:26:17 GMT Location: http://localhost:8181/cxf/api/people/a@b.com Server: Jetty(8.1.14.v20131031) And verify that person has been created successfully: curl -i http://localhost:8181/cxf/api/peopleHTTP/1.1 200 OK Content-Type: application/json Date: Sat, 09 Aug 2014 15:28:20 GMT Transfer-Encoding: chunked Server: Jetty(8.1.14.v20131031)[{"email":"a@b.com","firstName":"Tom","lastName":"Knocker"}] Would be nice to check if database has the person populated as well. With Apache Karaf 3.0.1 shell it is very simple to do by executing just two commands: jdbc:datasources and jdbc:query peopleDb “select * from people”.Awesome! I hope this quite introductory blog post opens yet another piece of interesting technology you may use for developing robust, scalable, modular and manageable software. We have not touched many, many things but these are here for you to discover. The complete source code is available on GitHub. Note to Hibernate 4.2.x / 4.3.x users: unfortunately, in the current release of Apache Karaf 3.0.1 the Hibernate 4.3.x does work properly at all (as JPA 2.1 is not yet supported) and, however I have managed to run with Hibernate 4.2.x, the container often refused to resolve the JPA-related dependencies.Reference: OSGi: the gateway into micro-services architecture from our JCG partner Andrey Redko at the Andriy Redko {devmind} blog....

IDE: 5 most favorite NetBeans features

Development with pleasure…. NetBeans has many interesting features that make development very easy, with few steps, and makes for a very productive environment, in terms of getting your product to market very quickly. It is very hard to limit my talking to only five features, while this IDE has tons of interesting features. But here I am going to mention my most favorite 5 features I am daily using in my development process on many platforms:      It is the reference development implementation IDE: I am a JCP (Java Community Process), EC (Executive Committee), a JSRs (Java Specifications Request) adopter and FishCAT (Glassfish Community Acceptance Testing) contributor especially in Java, SE & EE world. While I am in early phases of testing the specifications, I need an IDE which support those technologies and made it easy to test and develop them, to report bugs, enhancements and confirms that JSRs Specs are valid against RIs, and with other JSRs on latest RI Application server like Glassfish.NetBeans offered all of this for us to streamline the process, with almost a nightly and weekly builds alongside JSRs RIs builds, which we use to test previously mentioned RIs betas.The most interesting part of the story here is when we released Java EE 7 on last Mid of June 2013; there was already NetBeans 7.4 was generally available released to fully support development of Java EE 7 technologies. And this happens also for JDK 8 when it released 18-March 2014, there was NetBeans 8.0 fully support Java SE 8.0.Android Mobile development made easy: NetBeans shipped with a very great support for mobile development either it was pure Android development in term of this is a Java Based IDE or Hybrid development in term of frontend technology support (HTML5, CSS3 and JavaScript).With a very productive plugin NBAndroid you can develop a pure Android project compatible with original ADT SDK (Android Development Toolkit) project structure, test, emulate and deploy on emulators as well as native devices at the same time. You should give it a try if you interested in mobile development. Create a new Project:Identify Properties:Created project:Runs On Android Emulator:Hybrid Mobile development support: Hybrid mobile development is never made easy as now in NetBeans nowadays, NetBeans introduced a hybrid mobile development based on Cordova open source mobile framework. Which is one of the most popular frameworks to develop hybrid mobile applications.While NetBeans gives a great and solid support for frontend engineering technologies like JavaScript, HTML5, CSS3, Frameworks like AngularJS, NodeJS, and jQuery core, mobile, and UI … etc.Which made it easy to develop a Hybrid based mobile applications for both Android and iOS in one shoot with few clicks. Without context-switching to other IDEs to accomplish the task.It has complete support for Apple iOS development toolkit and Android ADT SDK, therefore you can test your application locally for mobile web and on emulators/devices of both mobile OSs, then export final product either it was .apk binary for android or .ipa for iOS, for final deployment on Apple store or Google play store. Creating the project:Choosing template:Identifying project properties:Choosing deployment method:Deployed on iOS simulator:JavaScript libraries in one place: If you are a frontend engineer, then you got the right choice, yes this IDE is for you, I know you need to develop a modern web application, but wait this needs many JavaScript modules and libraries to do it.One among the very panic tasks in web development nowadays, is to search for JavaScript tools and setup your project for them, then download each JavaScript libs from different sites with there resources and support libs, in addition you have also to manage their updates to fix problems or get a new feature. NetBeans comes with a build in tool that allows you to get your JavaScript libraries in one shoot from the IDE directly and mange their updates from the same place.You will find almost all JavaScript libs you need, and you can filter your choice from the tool to pic up the right libs you need for your project, and where to put them in your project.As mentioned in first picture the libs were updated on Apr 26, 2014, just click and will updates all libs to today releases.It saves me a bunch of site visiting and worrying about updating them, and even adding new ones in future of the project. The JavaScript tool:Pickup and filter your choices:SOAP based client/Web Services in one click: I am working in government environment, which requires much integration between systems either directly or through a G2G (Government-to-Government) hub, each one needs a specific integration requirements.If it is directly, you have to define you web service annotations on interface based on your backend EJB session bean interface or separate endpoint interface to decouple your Endpoint from your façade, then generate WSDL to be consumed by others.Other scenarios is to go to a Top-Down approach, in developing WSDL first to be certified on G2G hub based on their standards, then generate service endpoint interface from them.Lately I have been asked to develop a Pension G2G WSDL definition and at the same time generate endpoint service interface to begun the testing in one week.I have accomplished this task in 3 days using NetBeans, it has a tremendous support for XML, XSD development, and after finishing the WSDL definition, I need to confirm a correct endpoint service creation, Before publishing service WSDLs to G2G authors.Okay from NetBeans I have created a Web Service from WSDL, passed the WSDL and click finish, that’s all.The surprise was it generates the service endpoint correctly as WSDL def. and at the same time created Service ports and client to test service in one shoot.All I have to do is to implements my service endpoint operations and huryaaaa finished my task. Adding WSDL and Identifies project attributes:Generated Service endpoint:Thanks for NetBeans team for such support and made it a development with pleasure.Reference: IDE: 5 most favorite NetBeans features from our JCG partner Mohamed Taman at the Improve your life Through Science and Art blog....

Applying decorator pattern to decorate views on Android

Hi there! Today i’m gonna show how to apply the design pattern decorator to android’s views making use of its dependency injection’s concept. I thought it was a nice, clear and simple way to decorate views and that’s my motivator to share it with you. I’m assuming you know the decorator pattern already and are looking for real examples involving android. If you are trying to understand the concept behind it, this is also a good article but you may need to learn the fundamentals first. There are a bunch of good books out there. I personally recommend head first. In this example i’ll try to show how to decorate a view with icons dynamically. Something similar to eclipse’s tree decoration while some compile error or warning appears on the package structure. We will end up with something like that:Error/warning tree decoration in eclipseOur view decoration example in Android  Let’s visualize the structure by defining the UML-diagramFirst create a blank android’s project Put some icons of your choice in the folder drawable-yourChoice. In my example i took the drawable-xhdpi. I took the icons directly from my eclipse package. You may use your own icons.   Layouting the view Ok, now copy this code into your activity_main.xml layout: < RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" >< ImageView android:id="@+id/middle" android:layout_width="200dp" android:layout_height="200dp" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:src="@drawable/ic_launcher" / >< ImageView android:id="@+id/leftTop" android:layout_width="30dp" android:layout_height="30dp" android:layout_alignLeft="@+id/middle" android:layout_alignTop="@+id/middle" / >< ImageView android:id="@+id/rightTop" android:layout_width="30dp" android:layout_height="30dp" android:layout_alignRight="@+id/middle" android:layout_alignTop="@+id/middle" / >< ImageView android:id="@+id/leftBottom" android:layout_width="30dp" android:layout_height="30dp" android:layout_alignBottom="@+id/middle" android:layout_alignLeft="@+id/middle" / >< ImageView android:id="@+id/rightBottom" android:layout_width="30dp" android:layout_height="30dp" android:layout_alignBottom="@+id/middle" android:layout_alignRight="@+id/middle" / >< ImageView android:id="@+id/middleTop" android:layout_width="30dp" android:layout_height="30dp" android:layout_alignTop="@+id/middle" android:layout_centerHorizontal="true" / >< / RelativeLayout > The common decorator interface Well ok, let’s start by defining the common decorator interface: /** * The common decorable interface to be used while implementing decorators. * @author Ricardo Ferreira * @version 1.0 * @since 07/04/2014 */ public interface Decorable { /** implements the concrete decorator's behavior */ void decorate(); } Defining the decorators contract This class defines what kind of object we want to decorate. In our case an android’s view which belongs to the Android’s API itself: import java.util.Arrays; import android.content.Context; import android.view.View; /** * Used to define the decorator's contract. In this case to decorate a view with icons * @author Ricardo Ferreira * @version 1.0 * @since 07/04/2014 */ public abstract class AbstractIconDecorator implements Decorable{ protected View view; protected View[] views; protected int drawableId; protected Context context; /** * Concrete classes must call this constructor to conform the decorator's contract. * @param context the app's context * @param view the view to be decorated * @param drawableId the drawable id dependency over R.drawable.yourId to be set * as background to the view of this constructor. */ public AbstractIconDecorator(Context context, View view, int drawableId){ super(); this.view = view; this.context = context; this.drawableId = drawableId; decorate(); } /** * Concrete classes must call this constructor to conform the undo decorator's contract. * @param context context the app's context * @param views the views to be undone. */ public AbstractIconDecorator(Context context,View[] views){ super(); this.context = context; this.views = Arrays.copyOf(views, views.length); decorate(); } } Implementing a master icon decorator This is a very nice implementation that takes advantage of android’s concept of dependency injection over the resource class R.drawable.myId. Using it, we don’t have to implement a lot of decorators. Instead of it, we implement just one and inject our dependencies according to our needs as you will see. import android.content.Context; import android.view.View; /** * Use this class to decorate views with icons thru dependency injection passing R.drawable.yourId * @author Ricardo Ferreira * @version 1.0 * @since 07/04/2014 */ public class IconDecorator extends AbstractIconDecorator{/** * Creates an universal icon decorator to be used while dealing with view's decoration. * @param context the app's context * @param view the view to be decorated * @param drawableId the drawable id dependency over R.drawable.yourId to be set * as background to the view of this constructor. */ public IconDecorator(Context context,View view, int drawableId){ super(context, view, drawableId); } @Override public void decorate() { view.setBackground(context.getResources().getDrawable(drawableId)); } } Implementing an undo decorator to revert decorations This is a special kind of decoration. I will decorate the main icon(the android’s green robot icon) with “nothing” making all decorations disappear. /** * Use this decorator to undo decoration by passing the view to be undone. * @author Ricardo Ferreira * @version 1.0 * @since 07/04/2014 */ public class ClearIconDecorator extends AbstractIconDecorator{/** * Creates an undo decorator. * @param context the app's context * @param views the views that has been decorated to be undone. */ public ClearIconDecorator(Context context,View[] views){ super(context,views); } @Override public void decorate() { for (View view : views) { view.setBackground(null); } } } The main activity Now the final step before running it. In this example i am simulating the decoration over clicks. But in your professional applications those decorations may occur while something go wrong or right or you may need to give some visual user feedbacks while doing something. Use your imagination! import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener;public class MainActivity extends Activity implements OnClickListener{private View middle; private View leftTop; private View leftBottom; private View rightBottom; private View rightTop; private View middleTop; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); middle = findViewById(R.id.middle); leftTop = findViewById(R.id.leftTop); leftBottom = findViewById(R.id.leftBottom); rightBottom = findViewById(R.id.rightBottom); rightTop = findViewById(R.id.rightTop); middleTop= findViewById(R.id.middleTop); leftTop.setOnClickListener(this); leftBottom.setOnClickListener(this); rightBottom.setOnClickListener(this); rightTop.setOnClickListener(this); middle.setOnClickListener(this); middleTop.setOnClickListener(this); }@Override public void onClick(View view) { switch (view.getId()) { case R.id.leftTop: new ErrorIconDecorator(view); break; case R.id.leftBottom: new WarningIconDecorator(view); break; case R.id.rightTop: new InfoIconDecorator(view); break; case R.id.rightBottom: new QuickFixIconDecorator(view); break; case R.id.middleTop: new IconDecorator(this,view, R.drawable.newpack_wiz); break; case R.id.middle: final View[] views = new View[]{leftTop,leftBottom,rightTop,rightBottom,middleTop}; new UndoIconDecorator(views); break; default: break; } } // you may define specific decorators to your convenience... // ... or you may use directly the universal decorator IconDecorator(...). // It is up to your. Here i'm showing both examples public class WarningIconDecorator{ public WarningIconDecorator(View view){ new IconDecorator(MainActivity.this, view, R.drawable.warning_obj); } } public class ErrorIconDecorator { public ErrorIconDecorator(View view){ new IconDecorator(MainActivity.this, view, R.drawable.error); } } public class InfoIconDecorator { public InfoIconDecorator(View view){ new IconDecorator(MainActivity.this, view, R.drawable.information); } } public class PackageDecorator { public PackageDecorator(View view){ new IconDecorator(MainActivity.this, view, R.drawable.package_obj); } } public class QuickFixIconDecorator { public QuickFixIconDecorator(View view){ new IconDecorator(MainActivity.this, view, R.drawable.quickfix_error_obj); } } public class ErrorWarningDecorator { public ErrorWarningDecorator(View view){ new IconDecorator(MainActivity.this, view, R.drawable.errorwarning_tab); } } public class UndoIconDecorator{ public UndoIconDecorator(View[] views){ new ClearIconDecorator(MainActivity.this, views); } } } So that’s all. Hope you like it.Reference: Applying decorator pattern to decorate views on Android from our JCG partner Ricardo Ferreira at the Clean Code Development – Quality Seal blog....

How to customize / change ActionBar font, text, color, icon, layout and so on with Android

Hi there! Today i’m gonna share a very common task with you. How to customize an ActionBar on Android. We will change layout, font, textsize, textcolor, add listener to it, navigate back, hide title and home icon. At the end we will have something like this:          Well, lets start by changing some API provided settings. Downloading Font First of all, go to 1001freefonts and donwload a font you want to apply to your text. It is free! After downloading, create a folder called font into the folder assets in your projetct’s root. (create it, if not exists already) It looks like this: projectname>assets>font>yourfont.fftCreating custom colors We want to change the color of our font also. So for this reason, create two colors in your strings.xml file like this: <color name="selected">#824986</color>     <color name="unselected">#E4DFEC</color> Creating custom ActionBar Layout As we want to customize our actionBar, we will need to create a customized layout for it. So in your layout folder, create a new layout.xml file called custom_action_bar.xml like this: < ?xml version="1.0" encoding="utf-8"? > < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/actionBar" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:paddingLeft="5dp" android:paddingTop="7dp" android:orientation="horizontal" > < TextView android:id="@+id/titleFragment1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Syntax" android:textSize="20sp" android:textColor="@color/selected" / >< TextView android:id="@+id/titleFragment2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:text="ionary" / > < / LinearLayout> Create createCutomActionBarTitle() in your Activity Create this method and enable setDisplayShowCustomEnable to true and setDisplayShowTitleEnable to false. Then inflate the above created custom_action_bar.xml file. Read your new downloaded font and set it to the textviews. At least add it to the actionBar. Done with the layout.     private void createCutomActionBarTitle(){         this.getActionBar().setDisplayShowCustomEnabled(true);         this.getActionBar().setDisplayShowTitleEnabled(false);        LayoutInflater inflator = LayoutInflater.from(this);         View v = inflator.inflate(R.layout.custom_action_bar, null);        Typeface tf = Typeface.createFromAsset(getAssets(),"font/yourfont.ttf");         ((TextView)v.findViewById(R.id.titleFragment1)).setTypeface(tf);         ((TextView)v.findViewById(R.id.titleFragment2)).setTypeface(tf);        //assign the view to the actionbar         this.getActionBar().setCustomView(v);     } Adding behaviors and navigate back funcions A disavantage of customized actionBars is that you have to worry about everything. Including default behaviors like navigating back and enabling actions on the new inserted TextViews. We will do this now. Normally you navigate from activity to activity and after doing something you’d like to give the possibility to the users to navigate back over touching the actionBars title. To do so, enhance the method createCustomActionBarTitle like this: private void createCutomActionBarTitle(){ this.getActionBar().setDisplayShowCustomEnabled(true); this.getActionBar().setDisplayShowTitleEnabled(false);LayoutInflater inflator = LayoutInflater.from(this); View v = inflator.inflate(R.layout.action_bar_contribution, null);Typeface tf = Typeface.createFromAsset(getAssets(),"font/fat_tats.ttf"); TextView frag1 = (TextView)v.findViewById(R.id.titleFragment1); frag1.setTypeface(tf); TextView frag2 = (TextView)v.findViewById(R.id.titleFragment2); frag2.setTypeface(tf); frag1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(YourCurrentActivity.this, YourTargetActivity.class)); finish(); } }); frag2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(YourCurrentActivity.this, YourTargetActivity.class)); finish(); } });//assign the view to the actionbar this.getActionBar().setCustomView(v); } Adding MetaData to the Manifest file We are almost done. To be able to navigate back, we need to write an entry in the manifest file. The “YourTargetActivity” is the activity that will be called, wenn we press the actionBars title. In our case it would look like this: < activity android:label="yourActivityLabelName" android:name="your.package.YourActivity" android:parentactivityname="your.package.YourTargetActivity" android:screenorientation="landscape" android:windowsoftinputmode="stateHidden" > < meta-data android:name="android.support.PARENT_ACTIVITY" android:value="your.package.YourTargetActivity" > < / meta-data> < / activity> That’s all! hope you like it! The application to download is here:Reference: How to customize / change ActionBar font, text, color, icon, layout and so on with Android from our JCG partner Ricardo Ferreira at the Clean Code Development – Quality Seal blog....

Builder Design Pattern Applied

Hi there! Today i’m gonna share the first of a brand new design pattern series i made. The builder design pattern is a very useful and common pattern while developing serious apps. In this post i’ll give a tiny builder pattern framework, so you can always come back here and get it to work with. A mnemonic, while dealing with builder pattern, is to think about customization. I always think about it, when i’m figuring out if i should use it or if i better take a factory. That’s the way it works better for me. try yourself.   The UML Here is how the little framework looks like. Simple, nice and straightforward.The code behind it The code is also very simple, small, clean and self-explanatory. I like to code expressive, so i don’t need to comment a lot. In this sample here i did, because it has a tutorial character. While developing i created some convention to my self. I think it is very important to do so. It is like applying the right grammar on a language while developing. For example: If i’m using the Builder Pattern, i always put the suffix Builder at the end. Well you may say or think now: what? But thats in fact a very, very, important info for beginners and expirienced developers. They will automatically see the idea behind it and will try not to break the pattern. In fact expirienced developers will love it and try to continue a good work, because they know, that the developer who wrote this, knows what he did and for sure there was a reason for it. So always try to be clear enough and give the right information on the right places. Someone else will thank you later.  But now to the code… // 1. EXAMPLE: PARTS OF THE CUSTOMIZABLE PRODUCT WE WANT public interface Part { // DEFINE THE METHODS YOUR PARTS WILL HAVE... void anyMethodNameYouLike(); }// 2. THE BUILDER METHOD WILL ADD // PARTS RETURNING THE BUILDER ITSELF public interface BuildContract < B > { B mount(Part part); }// 3. DEFINE THE BUILDER'S CONTRUCTION METHOD // WHICH BUILDS AND RETURNS THE FINAL PRODUCT "T" public interface Builder < T > extends BuildContract < Builder < T > > { T build(); } A real example Nothing better then that to fix it and understand it better. Let´s implement a cake bakery. A colleague of yours wants to open a bakery and asked you to program a bakery’s software for him. Let’s do it..! And by the way, I commend you heartily, using a UML diagram tool is as visualization mechanism to show your ideas and improve your design skills. Lets start by the UML:The analogy Let’s now use our tiny framework and make the analogy for our bakery. Ingredient is the Part, Recipe is the BuilderContract and  Builder is the builder itself. Cake is the final, customizable product. CakeBuilder is the class which actually creates the product after customization (after the addition of as many parts – ingredients – as you want). The client would be the final client or your colleague taking the order. Just use or imagination… Let’s dive into code now… The Ingredients (Parts) The parts in our example are the ingredients. Let’s implement some ingredients to use it later to make a cake. // 1. EXAMPLE: PART TO CUSTOMIZATE "INGREDIENTS" public interface Ingredient { // INGREDIENTS WILL HAVE... void printName(); String getUnitPrice(); void printCalories(); } public class LightMilk implements Ingredient {private int deciLiter; private int calories; private String unitPrice; public LightMilk(int deciLiter){this.deciLiter=deciLiter;} public LightMilk(int deciLiter, int calories, String unitPrice) { super(); this.deciLiter = deciLiter; this.calories = calories; this.unitPrice = unitPrice; }@Override public void printName() {System.out.printf(" Light Milk");} @Override public String getUnitPrice() {return unitPrice;} @Override public void printCalories() {System.out.printf(" 76kc");} public int getDeciLiter() {return deciLiter;} public void setDeciLiter(int deciLiter) {this.deciLiter = deciLiter;} public int getCalories() {return calories;} public void setCalories(int calories) {this.calories = calories;} public void setUnitPrice(String unitPrice) {this.unitPrice = unitPrice;} } public class Sugar implements Ingredient {private int gram; private int calories; private String unitPrice; public Sugar(int deciLiter){this.gram=deciLiter;} public Sugar(int gram, int calories, String unitPrice) { super(); this.gram = gram; this.calories = calories; this.unitPrice = unitPrice; }@Override public void printName() {System.out.printf(" Sugar");} @Override public String getUnitPrice() {return unitPrice;} @Override public void printCalories() {System.out.printf(" 40kc");} public int getGram() {return gram;} public void setGram(int gram) {this.gram = gram;} public int getCalories() {return calories;} public void setCalories(int calories) {this.calories = calories;} public void setUnitPrice(String unitPrice) {this.unitPrice = unitPrice;} } public class Choco implements Ingredient { private int gram; private int calories; private String unitPrice; public Choco(int gram, int calories, String unitPrice) { super(); this.gram = gram; this.calories = calories; this.unitPrice = unitPrice; } public int getGram() {return gram;} public void setGram(int gram) {this.gram = gram;} public int getCalories() {return calories;} public void setCalories(int calories) {this.calories = calories;} public void setUnitPrice(String unitPrice) {this.unitPrice = unitPrice;}@Override public void printName() {System.out.printf(" Chocolate");} @Override public void printCalories() {System.out.printf(" 389kc");} @Override public String getUnitPrice() {return unitPrice;} } public class NoSugar implements Ingredient {private int gram; private int calories; private String unitPrice; public NoSugar(int deciLiter){this.gram=deciLiter;} public NoSugar(int gram, int calories, String unitPrice) { super(); this.gram = gram; this.calories = calories; this.unitPrice = unitPrice; }@Override public void printName() {System.out.printf(" No Sugar");} @Override public String getUnitPrice() {return unitPrice;} @Override public void printCalories() {System.out.printf(" 0kc");} public int getGram() {return gram;} public void setGram(int gram) {this.gram = gram;} public int getCalories() {return calories;} public void setCalories(int calories) {this.calories = calories;} public void setUnitPrice(String unitPrice) {this.unitPrice = unitPrice;} } public class Milk implements Ingredient {private int deciLiter; private int calories; private String unitPrice; public Milk(int deciLiter){this.deciLiter=deciLiter;} public Milk(int deciLiter, int calories, String unitPrice) { super(); this.deciLiter = deciLiter; this.calories = calories; this.unitPrice = unitPrice; }@Override public void printName() {System.out.printf(" Milk");} @Override public String getUnitPrice() {return unitPrice;} @Override public void printCalories() {System.out.printf(" 128kc");} public int getDeciLiter() {return deciLiter;} public void setDeciLiter(int deciLiter) {this.deciLiter = deciLiter;} public int getCalories() {return calories;} public void setCalories(int calories) {this.calories = calories;} public void setUnitPrice(String unitPrice) {this.unitPrice = unitPrice;} } The Builder’s Contract This is the Recipe in our example. // 2. THE BUILDER METHOD WILL ADD // INGREDIENTS RETURNING THE BUILDER ITSELF public interface Recipe < B > { B addIngredient(Ingredient ingredient); } // 3. DEFINE THE BUILDER CONTRUCTION METHOD // WHICH BUILDS AND RETURNS THE FINAL PRODUCT "T" public interface Builder < T > extends Recipe < Builder < T > > { T build(); } import java.util.ArrayList; import java.util.List; // 4. IMPLEMENT THE BUILDER ACC. TO YOUR NEEDS public class CakeBuilder implements Builder < Cake > { // IN THIS CASE THE PARTS ARE THE INGREDIENTS private List < Ingredient > ingredients=new ArrayList < Ingredient > ( ); @Override public Cake build() { if(!ingredients.isEmpty()){ // THE FINAL PRODUCT IS A CHOCO-MUFFIN return new Cake(ingredients); } return new Cake(null); } @Override // BECAUSE I ALWAYS GET A BUILDER BACK, I'M ABLE TO // ADD A LOT OF PARTS BEFORE I CALL "BUILD()" public Builder < Cake > addIngredient(Ingredient ingredient) { if(ingredient!=null){ ingredients.add(ingredient); } return this; } } The product In our example the product to build is a cake. import java.util.List;public class Cake { public Cake(List < Ingredient > ingredients){ String muffin = ""; if(ingredients==null){ System.out.println(" zero cake "+muffin); return; } // PRINT OUT MUFFIN INGREDIENTS System.out.printf(" Cake with: "); for (Ingredient ingredient : ingredients) { ingredient.printName(); } // PRINT OUT PART PRICES for (Ingredient ingredient : ingredients) { muffin+=" "+ingredient.getUnitPrice();//NOPMD } System.out.println(" - Price: "+muffin); } public void printResult(){ System.out.println(" Cake is ready!"); } } Testing it Finally the client test. Here we can see the usage of it: // 5. TESTING THE CHOCO-BUILDER public class Client { public static void main(String[] args) { Builder < Cake > chocoMuffinBuilder = new CakeBuilder(); chocoMuffinBuilder.addIngredient(new Choco(10, 23, "3.39")); chocoMuffinBuilder.addIngredient(new Milk(34, 67, "1.57")); chocoMuffinBuilder.addIngredient(new Sugar(34, 67, "2.00")); final Cake chocoMuffin = chocoMuffinBuilder.build(); chocoMuffin.printResult(); } } That’s all! Hope you like it!Reference: Builder Design Pattern Applied from our JCG partner Ricardo Ferreira at the Clean Code Development – Quality Seal blog....

Game AI – An Introduction to Behaviour Trees

Game AI is a very broad subject and while there is a lot of material out there, I didn’t find something that introduces the concepts gently and at a slower, more understandable pace. This article will try to explain how to design a very simple but extendable AI system loosely based on the concept of Behaviour Trees. What is AI? Artificial Intelligence is the human-like behaviour exhibited by the entities participating in the game. It’s more the illusion of intelligence and thoughtful action performed by the entities than an actual intelligent reasoning driven behaviour. The goal is to try to fool the player into thinking that the other “intelligent” entities are controlled by humans and not by a machine. It’s easier said than done, but we can use a lot of tricks to achieve some really good, seemingly random and “intelligent” behaviours. An Example Before we jump right into the fun bit, let’s draft up a plan of what we want to achieve. Again, I will use droids as an example. Imagine an arena where droids will battle it out amongst them and whichever droid is the last one standing is the winner. The arena will be a board and droids will be placed randomly on it. We’ll make it a turn based game so we can follow the whole AI unfold, but it can be easily turned into a realtime game. The rules are simple:The board is a rectangle A droid can move one tile per turn in either direction to any of the adjacent unoccupied tiles A droid has a range and can fire at droids within its range The droids will have the usual attributes: damage they inflict and hit pointsFor the sake of simplicity we will use very simple structures. The application will have a Droid class and a Board class. A droid will have the following attributes that define it: public class Droid {final String name; int x; int y; int range; int damage; int health;Board board;public Droid(String name, int x, int y, int health, int damage, int range) { this.name = name; this.x = x; this.y = y; this.health = health; this.damage = damage; this.range = range; }public void update() { // placeholder for each turn or tick }/* ... */ /* getters and setters and toString() */ /* ... */ } The Droid is just a simple pojo with a few attributes. The attributes are self explanatory but here’s a short summary of them:name – the unique name of the droid, can be used for ID as well. x and y – the coordinates on the grid. health, damage and range – what it says. board – is the reference to the Board the droid is on along with other droids. We need this because the droid will make decisions by knowing its environment, which is the board<./li>There is also an empty update() method which is called each time the droid finishes its turn. If it’s a realtime game, the update method is called from the game loop, ideally from the game engine. There are also the obvious getters and setters and the toString() method which are omitted from the listing. The Board class is vey simple. public class Board {final int width; final int height;private List<Droid> droids = new ArrayList<Droid>();public Board(int width, int height) { this.width = width; this.height = height; }public int getWidth() { return width; }public int getHeight() { return height; }public void addDroid(Droid droid) { if (isTileWalkable(droid.getX(), droid.getY())) { droids.add(droid); droid.setBoard(this); } }public boolean isTileWalkable(int x, int y) { for (Droid droid : droids) { if (droid.getX() == x && droid.getY() == y) { return false; } } return true; }public List<Droid> getDroids() { return droids; } } It has a width and a height and it contains a list of droids. It also contains a few convenience methods to check if a droid is already present at a given coordinate and a method to easily add droids one by one. So far this is pretty standard. We can scatter a few droids on the board but they will not do anything. We can create the board, add some droids to it and start calling update(). They are only some dumb droids. Not so Dumb Droids To make a droid do something, we could implement the logic in its update() method. This is the method called every tick or in our case every turn. For example we want our droids to wander around the arena (board) and if they see an other droid in range, engage them and start firing at them until they die. This would be a very rudimentary AI but is still a AI. The pseudo code would look like this: if enemy in range then fire missile at it otherwise pick a random adjacent tile and move there This means any interaction between the droids will result in a stand-off and the weaker droid gets destroyed. We might want to avoid this. So we can add that if a droid is likely to lose, then try to flee. Stand and fight only if there is nowhere to escape.if enemy in range then if enemy is weaker then fight otherwise if escape route exists then escape otherwise fight otherwise wanderThis is all good. The droids will start acting “intelligently” but they are still very limited unless we add more code to do more clever things. And also, they will act the same. Imagine if you drop them in a more complicated arena. An arena where there are items like power-ups to pick up to enhance powers, obstacles to avoid. For example decide between picking up a health/repair kit and picking up a weapon power-up when droids are swarming around. It can get out of hands quite quickly. Also what if we want a differently behaving droid. One is an assault droid and the other one is a repair droid. We could achieve this of course with object composition, but the brains of the droids will be extremely complicated and any change in game design will require tremendous effort to accommodate. Let’s see if we can come up with a system that can solve these problems. Here Comes the Brain We can think of the AI module of the droid as some kind of a brain. The brain is composed of several routines that act on the droid following a set of rules. These rules govern the execution of the routines so it maximises the chances of survival and winning the game as an ultimate goal. If we think of our human brain consisting of routines and having Maslow’s hierarchy of needs as a reference , we can identify a few routines instantly.Physiological routine – a routine that needs to be executed every time otherwise there won’t be any living Subsistence routine – this routine needs to be executed once the living conditions have been met so the long term existence is secured Aspirational routine – this will be executed if there is time left after the subsistence has been secured and before subsistence needs to be executed againLet’s break the human intelligence down a bit. A human needs to breathe to live. Each breath consumes energy. One can breathe so much until it runs out of energy. To replenish the energy one needs to eat. One can only eat if he/she has food at his/her disposal. If there is no available food, it needs to be acquired which consumes some more energy. If the procurement of food takes a long time (needs to be hunted for example) and the amount of food obtained is small, after eating it, the human is in need of more food and the routine restarts without delay. If the food was bought in bulks from a supermarket, after eating it, there is plenty left so the human can move on to do more interesting things which are in his/her aspirational section. Things like making friends or wage war or watch television for example. Just think of how many things are in a human brain to make us function and try to simulate it. This all by disregarding most of the stimuli we are getting and responding to them. To do this, we would need to parametrise the human body and each sensor triggered by a stimulus will update the correct parameters and the executed routine will examine the new values and act accordingly. I won’t describe it now but you get the idea I hope. Let’s switch back to our much simpler droids. If we try to adapt the human routines to the droids we’ll get something like this:Physiological / Existential – this part we can ignore for this example because we design robots and they are mechanical beings. Of course for them to exist, they still need energy (eg. action points) which they can take from a battery or from some other energy source which can be depleted. For the sake of simplicity we will ignore this and consider the energy source to be infinite. Subsistence / Safety – this routine will make sure that the droid will survive the current turn and live by avoiding an immediate threat. Aspirational – this kicks in once the safety routine checked out OK and didn’t have to activate the droid’s fleeing routine. The current simple aspiration for the droid is to kill the other droids.Although the described routines are very simple and can be hard-coded, the approach we will take to implement is a bit more elaborate. We will use an approach based on behaviour trees. First and foremost we need to delegate every activity of the droid to its brain. I will call it Routine instead of brain. It can be called Brain or AI or anything, but I have chosen Routine because it will serve as a base class for all the routines that will consist of. Also it will be in charge of governing the flow of information in the brain. The Routine itself is a finite state machine with 3 states. public abstract class Routine {public enum RoutineState { Success, Failure, Running }protected RoutineState state;protected Routine() { }public void start() { this.state = RoutineState.Running; }public abstract void reset();public abstract void act(Droid droid, Board board);protected void succeed() { this.state = RoutineState.Success; }protected void fail() { this.state = RoutineState.Failure; }public boolean isSuccess() { return state.equals(RoutineState.Success); }public boolean isFailure() { return state.equals(RoutineState.Failure); }public boolean isRunning() { return state.equals(RoutineState.Running); }public RoutineState getState() { return state; } } The 3 states are:Running – the routine is currently running and will act on the droid in the next turn. Eg. the routine is responsible to move the droid to a certain position and the droid is in transit and still moving uninterrupted. Success – the routine has finished and it succeeded doing what it was meant to be doing. For example if the routine is still the “move to position”, it succeeded when the droid reached the destination. Failure – using the previous example (move to), the moving of the droid was interrupted (either the droid got destroyed or some unexpected obstacle appeared or some other routine interfered) and it didn’t reach the destination.The Routine class has the act(Droid droid, Board board) abstract method. We need to pass in the Droid and the Board because when the routine acts, it does so on the droid and in the knowledge of the droid’s environment which is the board. For example the moveTo routine will change the droid’s position each turn. Usually when the routine acts on the droid, it uses the knowledge gathered from its environment. This knowledge is modelled on the realities of the situation. Imagine that the droid (like us humans) can’t see the whole world but only as far as its sight range. Us humans have a field of view of about 135 degrees so if we would be simulating a human, we’d pass in a slice of the world containing the section we see and all the visible components in it and let the routine process just that to the best of its capabilities and come to a conclusion. We could do that for the droids too, and just pass in the section of the board that is covered by the range, but we will keep it simple for now and use the whole board. The start(), succeed() and fail() methods are simple public overridable methods that set the state accordingly. The reset() method on the other hand is abstract and it has to be implemented by each concrete routine to reset any internal state proprietary to that routine. The rest are convenience methods to query the state of the routine. Learning to Walk Let’s implement the first concrete routine which will be the MoveTo discussed above. public class MoveTo extends Routine {final protected int destX; final protected int destY;public MoveTo(int destX, int destY) { super(); this.destX = destX; this.destY = destY; }public void reset() { start(); }@Override public void act(Droid droid, Board board) { if (isRunning()) { if (!droid.isAlive()) { fail(); return; } if (!isDroidAtDestination(droid)) { moveDroid(droid); } } }private void moveDroid(Droid droid) { if (destY != droid.getY()) { if (destY > droid.getY()) { droid.setY(droid.getY() + 1); } else { droid.setY(droid.getY() - 1); } } if (destX != droid.getX()) { if (destX > droid.getX()) { droid.setX(droid.getX() + 1); } else { droid.setX(droid.getX() - 1); } } if (isDroidAtDestination(droid)) { succeed(); } }private boolean isDroidAtDestination(Droid droid) { return destX == droid.getX() && destY == droid.getY(); } } It’s a very basic class that will move the droid one tile towards the destination until it reaches it. It does not check for any other constraint than if the droid is alive. That’s the condition for failure. The routine has 2 parameters destX and destY. These are final attributes which the MoveTo routine will use to achieve its target. The routine’s single responsibility is to move the droid. If it can’t do that it will fail. That’s it. Single responsibility is very important here. We will see how we’ll combine these to achieve more complex behaviours. The reset() method simply sets the status to Running. It has no other internal state or values to deal with, but it needs to be overridden. The heart of the routine is the act(Droid droid, Board board) method which performs the action and contains the logic. First it checks for the failure condition, which is if the droid is dead. If it’s dead and the routine is active (its status is Running) then the routine failed to do what it was supposed to. It calls the super class’s default fail() method to set the status to Failure and exits the method. The second part of the method checks for the success condition. If the droid is not yet at the destination, then move the droid one tile towards the destination. If its’ reached the destination, set the state to Success. The check for isRunning() is made to make sure that the routine only acts if the routine is active and hasn’t finished. We also need to fill in the Droid‘s update method to make it use the routine. It’s just a simple delegation. This is how it looks like: public void update() { if (routine.getState() == null) { // hasn't started yet so we start it routine.start(); } routine.act(this, board); } It should consist only of line #6 but I also put in a check to see if the state is null and if so, then start the routine. This is a hack to start the routine the first time update is called. It’s a quasi command pattern, as in the act method gets the receiver of the action command as a parameter, which is the droid itself. I also modified the Routine class to log the different events in it, so we can see what’s happening. // --- omitted --- */ public void start() { System.out.println(">>> Starting routine: " + this.getClass().getSimpleName()); this.state = RoutineState.Running; }protected void succeed() { System.out.println(">>> Routine: " + this.getClass().getSimpleName() + " SUCCEEDED"); this.state = RoutineState.Success; }protected void fail() { System.out.println(">>> Routine: " + this.getClass().getSimpleName() + " FAILED"); this.state = RoutineState.Failure; } // --- omitted --- */ Let’s put it to test with a simple Test class. public class Test {public static void main(String[] args) { // Setup Board board = new Board(10, 10);Droid droid = new Droid("MyDroid", 5, 5, 10, 1, 2); board.addDroid(droid);Routine moveTo = new MoveTo(7, 9); droid.setRoutine(moveTo); System.out.println(droid);// Execute 5 turns and print the droid out for (int i = 0; i < 5; i++) { droid.update(); System.out.println(droid); } } } It’s a standard class with a main method which firsts sets up a square 10 x 10 Board and adds a Droid with the provided attributes at the coordinates 5,5. On line #10 we create the MoveTo routine which sets the destination to (7,9). We set this routine to be the only routine of the droid (line #11) and print the droid’s state (line #12). Then we execute 5 turns and display the droid’s state after each turn. Running the Test we see the following printed to the sysout: Droid{name=MyDroid, x=5, y=5, health=10, range=2, damage=1}>>> Starting routine: MoveToDroid{name=MyDroid, x=6, y=6, health=10, range=2, damage=1}Droid{name=MyDroid, x=7, y=7, health=10, range=2, damage=1}Droid{name=MyDroid, x=7, y=8, health=10, range=2, damage=1}>>> Routine: MoveTo SUCCEEDEDDroid{name=MyDroid, x=7, y=9, health=10, range=2, damage=1}Droid{name=MyDroid, x=7, y=9, health=10, range=2, damage=1} As we can see the droid starts at position (5,5) as expected. Calling the update method for the first time, starts the MoveTo routine. The subsequent 3 calls to the update will move the droid to the destination by changing its coordinate each turn by one. After the routine succeeded, all the calls passed to the routine are ignored, because it’s complete. This is the first step, but it’s not very helpful. Let’s say we want to have our droid wander around the board. To achieve this, we will need to execute the MoveTo routine repeatedly, but every time it restarts, the destination needs to be randomly picked. Wandering About But let’s start with the Wander routine. It’s nothing more than a MoveTo but we generate a random destination given that we know the board. public class Wander extends Routine {private static Random random = new Random(); private final Board board; private MoveTo moveTo;@Override public void start() { super.start(); this.moveTo.start(); }public void reset() { this.moveTo = new MoveTo(random.nextInt(board.getWidth()), random.nextInt(board.getHeight())); }public Wander(Board board) { super(); this.board = board; this.moveTo = new MoveTo(random.nextInt(board.getWidth()), random.nextInt(board.getHeight())); }@Override public void act(Droid droid, Board board) { if (!moveTo.isRunning()) { return; } this.moveTo.act(droid, board); if (this.moveTo.isSuccess()) { succeed(); } else if (this.moveTo.isFailure()) { fail(); } } } Following the single responsibility principle, the Wander class’s sole purpose is to pick the random destination on the board. Then it uses the MoveTo routine to get the droid to the new destination. The reset method will restart it and pick a new random destination. The destination is set in the constructor. If we’d like our droid to wander, we would change the Test class to the following: public class Test { public static void main(String[] args) { // Setup Board board = new Board(10, 10);Droid droid = new Droid("MyDroid", 5, 5, 10, 1, 2); board.addDroid(droid);Routine routine = new Wander(board); droid.setRoutine(routine); System.out.println(droid);for (int i = 0; i < 5; i++) { droid.update(); System.out.println(droid); } } } The output would be something similar to this: Droid{name=MyDroid, x=5, y=5, health=10, range=2, damage=1}>>> Starting routine: Wander>>> Starting routine: MoveToDroid{name=MyDroid, x=6, y=6, health=10, range=2, damage=1}Droid{name=MyDroid, x=7, y=7, health=10, range=2, damage=1}Droid{name=MyDroid, x=7, y=8, health=10, range=2, damage=1}>>> Routine: MoveTo SUCCEEDED>>> Routine: Wander SUCCEEDEDDroid{name=MyDroid, x=7, y=9, health=10, range=2, damage=1}Droid{name=MyDroid, x=7, y=9, health=10, range=2, damage=1} Notice how the Wander contains and delegates to the MoveTo routine. Repeat, Repeat, Repeat … This is all good but what if we want the droid to be wandering repeatedly? We will make a Repeat routine which will contain a routine to be repeated. Also we will make this routine so that it can take in a parameter to specify how many times we want to repeat a routine. If it won’t take in a parameter, then it will repeat the containing routine forever or until the droid’s dead. public class Repeat extends Routine {private final Routine routine; private int times; private int originalTimes;public Repeat(Routine routine) { super(); this.routine = routine; this.times = -1; // infinite this.originalTimes = times; }public Repeat(Routine routine, int times) { super(); if (times < 1) { throw new RuntimeException("Can't repeat negative times."); } this.routine = routine; this.times = times; this.originalTimes = times; }@Override public void start() { super.start(); this.routine.start(); }public void reset() { // reset counters this.times = originalTimes; }@Override public void act(Droid droid, Board board) { if (routine.isFailure()) { fail(); } else if (routine.isSuccess()) { if (times == 0) { succeed(); return; } if (times > 0 || times <= -1) { times--; routine.reset(); routine.start(); } } if (routine.isRunning()) { routine.act(droid, board); } } } The code is easy to follow but I’ll explain a few things that were added. The attribute routine is passed in the constructor and that will be the routine that gets repeated. The originalTimes is a storage variable that holds the initial number of times value, so we can restart the routine with the reset() call. This is just a back-up of the initial state. The times attribute is how many times the provided routine will be repeated. If it’s -1 then it’s infinite. This is all coded in the logic inside the act method. To test this out, we need to create a Repeat routine and provide what to repeat. For example, to have the droid wander endlessly, we’d have this: Routine routine = new Repeat((new Wander(board))); droid.setRoutine(routine); If we’d call the update repeatedly, we’ll see that the droid will be moving constantly. Check this sample output: Droid{name=MyDroid, x=5, y=5, health=10, range=2, damage=1}>> Starting routine: Repeat>>> Starting routine: Wander>>> Starting routine: MoveToDroid{name=MyDroid, x=4, y=6, health=10, range=2, damage=1}>>> Routine: MoveTo SUCCEEDED>>> Routine: Wander SUCCEEDEDDroid{name=MyDroid, x=4, y=7, health=10, range=2, damage=1}>>> Starting routine: Wander>>> Starting routine: MoveToDroid{name=MyDroid, x=5, y=6, health=10, range=2, damage=1}Droid{name=MyDroid, x=6, y=5, health=10, range=2, damage=1}Droid{name=MyDroid, x=7, y=4, health=10, range=2, damage=1}Droid{name=MyDroid, x=8, y=3, health=10, range=2, damage=1}Droid{name=MyDroid, x=8, y=2, health=10, range=2, damage=1}>>> Routine: MoveTo SUCCEEDED>>> Routine: Wander SUCCEEDEDDroid{name=MyDroid, x=8, y=1, health=10, range=2, damage=1}>>> Starting routine: Wander>>> Starting routine: MoveToDroid{name=MyDroid, x=7, y=2, health=10, range=2, damage=1}Droid{name=MyDroid, x=6, y=3, health=10, range=2, damage=1} Notice how the Repeat routine does not end. Assembling the Intelligence So far we’re just composing behaviours. But what if we want to add decision making to the droids and build a more complex behaviour? Enter the behaviour trees. This term doesn’t describe what it is and neither do most of the articles that I found. I’ll start with what I want to achieve first and hopefully it will all make sense. I want to implement the behaviour described at the beginning of the article. I want my droid to scan if there is a weaker droid in its range and engage it if it is, or flee otherwise. Take a look at the following diagram. It shows a tree. It’s nothing more than a routine composed of multiple different routines. Each node is a routine and we will have to implement some special routines.Let’s break the routines down.Repeat – is the routine implemented earlier. It will repeat the given routine forever or until the embedded routine fails. Sequence – the sequence routine will succeed only when all the routines that it contains have succeeded. For example to attack a droid, the enemy droid needs to be in range, the gun needs to be loaded and the droid needs to pull the trigger. Everything in this order. So the sequence contains a list of routines and acts on them until all succeed. If the gun is not loaded then there is no point in pulling the trigger so the whole attack is a failure. Selector – this routine contains a list of one or more routines. When it acts, it will succeed when one of the routines in the list succeeds. The order in which the routines are executed is set by the order in which the routines are passed in. If we’d like to randomise the execution of routines, it’s easy to create a Random routine whose sole purpose is to randomise the list of routines passed in. All the grey routines are leaves in the tree, meaning that they can’t have any subsequent routines and these are the ones that act on the droid which is the receiver.The above tree represents the very basic AI we wanted to implement. Let’s follow it through starting from the root. Repeat – will repeat the selector indefinitely until neither of the branches can be executed successfully. The routines in the selector are: Attack a droid and Wander. If both fail that means that the droid is dead. The Attack a droid routine is a sequence of routines meaning that all of them have to succeed in order for the whole branch to succeed. If it fails, then the fall back is to pick a random destination through Wander and to move there. Then repeat. All we need to do is to implement the routines. For example the IsDroidInRange could look something like this: public class IsDroidInRange extends Routine {public IsDroidInRange() {}@Override public void reset() { start(); }@Override public void act(Droid droid, Board board) { // find droid in range for (Droid enemy : board.getDroids()) { if (!droid.getName().equals(enemy)) { if (isInRange(droid, enemy)) { succeed(); break; } } } fail(); }private boolean isInRange(Droid droid, Droid enemy) { return (Math.abs(droid.getX() - enemy.getX()) <= droid.getRange() || Math.abs(droid.getY() - enemy.getY()) < droid.getRange()); } } It’s a very basic implementation. The way it determines if a droid is in range, is by iterating through all the droids on the board and if the enemy droid (assuming that the names are unique) is within range, then it succeeded. Otherwise it failed. Of course we need to feed in this droid to the next routine somehow, which is IsEnemyStronger. This can be achieved by giving the droid a context. One simple way could be that the Droid class could have an attribute nearestEnemy and on success the routine will populate that field and on failure it will clear it. This way the following routine can access the droid’s internals and use that information to work out its success or failure scenarios. Of course this can be and should be extended so the droid will contain a list of droids in its range and have a routine to work out if the droid should fly or fight. But it’s not the scope of this introduction. I won’t be implementing all the routines in the article, but you will be able to check out the code on github: https://github.com/obviam/behavior-trees and I will be adding more and more routines. Where to go from here? There are quite a few improvements that can be made just by looking at it. As a first step to test the system out, I’d move the creation of the routines to a factory for convenience. /** * Static convenience methods to create routines */ public class Routines {public static Routine sequence(Routine... routines) { Sequence sequence = new Sequence(); for (Routine routine : routines) { sequence.addRoutine(routine); } return sequence; }public static Routine selector(Routine... routines) { Selector selector = new Selector(); for (Routine routine : routines) { selector.addRoutine(routine); } return selector; }public static Routine moveTo(int x, int y) { return new MoveTo(x, y); }public static Routine repeatInfinite(Routine routine) { return new Repeat(routine); }public static Routine repeat(Routine routine, int times) { return new Repeat(routine, times); }public static Routine wander(Board board) { return new Wander(board); }public static Routine IsDroidInRange() { return new IsDroidInRange(); }} This will allow to test some scenarios out in a more elegant way. For example to place 2 droids with different behaviours you could do the following: public static void main(String[] args) { Board board = new Board(25, 25); Droid droid1 = new Droid("Droid_1", 2, 2, 10, 1, 3); Droid droid2 = new Droid("Droid_2", 10, 10, 10, 2, 2);Routine brain1 = Routines.sequence( Routines.moveTo(5, 10), Routines.moveTo(15, 12), Routines.moveTo(2, 4) ); droid1.setRoutine(brain1);Routine brain2 = Routines.sequence( Routines.repeat(Routines.wander(board), 4) ); droid2.setRoutine(brain2);for (int i = 0; i < 30; i++) { System.out.println(droid1.toString()); System.out.println(droid2.toString()); droid1.update(); droid2.update(); } } Of course this is not the best solution by far but it’s still better than the constant instantiation of routines. Ideally the AI should be scripted and loaded from an external source, either via scripting, or at least provided as a JSON for example and have a AI assembler create it. This way the game does not need to be recompiled every time the AI is tweaked. But again, it is not the scope of this article. Also, how shall we decide which action will take up a turn/tick or is evaluated instantly? One possible solution could be to allocate action points to the droid that it can spend one turn (tick if realtime) and for each action to assign a cost. Whenever the droid runs out of points, then we can move on. We’d also need to track which routine is the current one so we can optimise the traversal of the tree. This is helpful if the AIs are very complex especially in realtime games. If you think the article was useful and want to get the code, please check the github repo. You can also check back because I intend to extend it and to update it so it evolves into a more complete AI example. As it is my first encounter with AI, there will be lots of things to improve and I am always open to criticism and ideas on how to improve.https://github.com/obviam/behavior-treesReference: Game AI – An Introduction to Behaviour Trees from our JCG partner Impaler at the Against the Grain blog....

Books That Have Most Influenced My Software Development Career

I have read numerous books on software development and have learned valuable things from most of them, but a small subset of these books has significantly influenced me and how I develop software. Effective C++ and More Effective C++ Although I had written a lot of lines of code in BASIC, Pascal, C, and C++ before starting my career, it was C++ that was my first professional programming language. As such, it was the first programming language in which I started to get past the basic syntax and learn about more advanced issues and nuances of the programming languages. For the first time, I started considering performance issues, maintainability, readability, and issues other than just whether the code worked or not. Effective C++ and More Effective C++ helped me to see that there was more to programming than basic syntax and getting functionality to work. These books helped me to realize that there are general programming concepts that are wise to follow but that there are also language-specific concepts and tips one should learn and apply in the programming language of choice. General software development principles apply across many languages, but sometimes it is necessary to know language-specific features as well.Effective Java, Editions 1 and 2 After such good experiences with Effective C++ and More Effective C++, I was excited to learn about the book called Effective Java as I was transitioning from C++ to Java, a transition several years in the making. Effective Java did not disappoint. I started reading Effective Java sooner in my Java experience than I had started reading the Scott Meyer‘s C++ books in my C++ experience and it helped me tremendously. It helped me to see from the beginning that there were more considerations when developing with Java than just writing syntax and making things work.Java Cookbook Although I have purchased and read seemingly countless Java-themed books over the years, I’ve never had a book focused on the Java language basics. Instead, I’ve always had specialized books focusing on aspects of Java or Java EE. The way that I learned the basics of the Java programming language was via the Java Cookbook. It helped me realize that I like this recipe-based approach to reading about, learning, and applying a new programming language. I’ve liked recipe-oriented books ever since, especially for quickly learning and apply a new programming language or framework.Groovy Recipes: Greasing the Wheels of Java Groovy Recipes: Greasing the Wheels of Java was my introduction to Groovy. This excellent book provides a nice introduction to Groovy that not only quickly introduces its syntax and features but also introduces practical ways that Groovy can be applied in regular everyday situations. I was able to quickly see the value of learning Groovy and to realize that Groovy was more than just a trendy language fad. This book reaffirmed my realization that I learn programming languages best from recipe-oriented books that demonstrate the language used in practical situations. Groovy Recipes: Greasing the Wheels of Java and the Groovy language itself have dramatically influenced my overall approach to software development by giving me the tools and confidence to do significantly more scripting than I used to do. Being able to script and develop software in similar languages (Groovy and Java) and use the same Java libraries and frameworks has made scripting easier. Easier scripting has meant that I’ve done more of it and spent less time doing it.Holub on Patterns: Learning Design Patterns by Looking at Code I purchased Holub on Patterns after seeing Allen Holub speak at SD West 2005 (I wasn’t the only one). Holub, the author of Why Extends is Evil and Why Getters and Setters Are Evil, challenged my way of thinking (traditional and commonly accepted way of thinking at the time) about object-oriented programming. I definitely look at objects now more from a behavioral perspective than data perspective. Also, although willing to write get/set methods when necessary, my reluctance to write them until needed has been of great benefit as the era of multicores has helped us realize the dangers of mutable state. Although Holub on Patterns does provide in-depth coverage of some common design patterns implemented in Java, the far bigger influence it had on me was in how I think about object-oriented design.Expert One-on-One J2EE Design and Development At a time when developers of enterprise Java applications were being told to use heavyweight EJBs and like it, many of us were wondering what we were missing when it seemed overly complex to us. Rod Johnson‘s J2EE Design and Development stuck a chord with me; it was relieving to see that I wasn’t the only one who thought EJB 1.x was too difficult and cumbersome. More importantly, Johnson’s book provided examples and discussion text that helped to see what parts of J2EE were worth using and which parts needed improvement. The Spring Framework was born out of the examples covered in this book and Spring’s success inspired changes in Java EE that make it much more productive and useful. J2EE Design and Development influenced me personally and influenced an entire community. It also helped me to realize that one can be correct in thinking something isn’t all it’s said to be even if the so-called experts insist it is. This has helped me weather numerous fads (and their blindly adherent evangelists) in software development since then. The book also articulated better than I could my own thoughts and feelings on enterprise Java development.Agile Web Development with Rails During my brief infatuation with Ruby on Rails, I found Agile Web Development with Rails to be a great introduction to Ruby on Rails. Although I enjoyed my foray into Rails, I have never worked with it to the same degree as I have with Java. Agile Web Development with Rails introduced me to Ruby on Rails, but more importantly it influenced my career, which has mostly not been in Rails, by demonstrating in a practical way how agile development can be done if a client is readily available to provide feedback. Agile Web Development with Rails and the Ruby on Rails framework itself helped change my way of thinking to see the value of convention over configuration. Shortly thereafter, the Java-sphere began to see changes inspired by Ruby on Rails’s success. These included Java Persistence API (JPA) adopting “configuration by exception” approach and the Grails framework. Ruby on Rails influenced much of the web development community and Agile Web Development with Rails was the tool that allowed me to most easily experience this influence personally.The Practice of Programming The Practice of Programming does not waste time with extraneous pages. The 250 page book covers the most fundamental issues in programming such as debugging, testing, performance, and portability. I read this early in my software development career and find myself reading portions of it again from time to time. Many great books have come along since that deal with these subjects, but The Practice of Programming was the first to cover most of these for me.Software Craftsmanship: The New Imperative Software Craftsmanship: The New Imperative was published at almost the perfect time for me in my career. The book helped me articulate my own observations about what made a “software engineer” more complete. In addition, this relatively short and to-the-point book helped me discover other approaches that I would later use in efforts to make my code more than just functional.201 Principles of Software Development 201 Principles of Software Development features 201 short (typically one or two paragraphs) descriptions of 201 software development principles with a reference to a generally well-recognized expert source with further details on that principle. The text is comprehensive enough to understand the principle and the references to proven sources give the reader the ability to learn in more detail how to apply the covered principle. This book not only helped me to solidify these principles in my own mind, but helped me to understand the breadth of materials available to the software developer. During my software development career, I’ve repeatedly seen trends come and go. Sometimes I’ll hear a relatively new developer talking about some “new” thing that is really just a reincarnation of the same idea in different form that its previous incarnations. 201 Principles of Software Development helped me realize at a relatively early stage in my career that many concepts and principles that were “new” to me, were really not new to the greater software development industry. Most “innovative” concepts in our industry typically share common themes or are even grounded in one or more of the principles covered in this book.Conclusion The books covered in this post have had significant influence on my software development career and how I think about software development today. Numerous other books have had lesser degree of influence. In some cases, these other books might have had more influence had I encountered them at a different (often earlier) point in my career. In fact, that might be the point of this post: different books will have different impact on different developers depending on where those developers are in their careers. The books in this post have had particularly great influence on me, but I know that other developers have been significantly influenced by books such as Code Complete, Clean Code, and numerous other books. I was surprised at how many language-specific books were on this list as I compiled it, but each was really more about that language. These books are about how to apply the language to achieve more general software development ideals and that’s where they have been most influential to me.Reference: Books That Have Most Influenced My Software Development Career from our JCG partner Dustin Marx at the Inspired by Actual Events blog....

Java EE: Asynchronous constructs and capabilities

Introduction Java EE has a number of APIs and constructs to support Asynchronous execution. This is vital from a scalability and performance stand point. Let us assume 2 modules which are interacting with each other. When moduleA (the sender) sends a message to moduleB (the receiver) in a Synchronous fashion, the communication takes place in the context of a Single thread i.e. the thread which initiated the communication from moduleA is blocked until moduleB responds back. This was a generic statement but can be extended to the context of a simple Java methods interacting with each other – in this case, a synchronous call from methodA to methodB would execute in the same thread which would be blocked until methodB returns or throws an exception. Why do we need Asynchronous behavior in Java EE based applications? We can further extrapolate this point to the Java EE world – be it inter server communication e.g. between web tier and EJB tier (servlets and EJBs) or a typical client server interaction – a browser interacting with a RESTful end point, Servlets etc – the server thread which responds to a client request always blocks until the server component responds back. Here is where Asynchronous execution comes into play – if the server thread processing the client request can be released/suspended and the actual business logic is executed in a separate thread (different than that of the original one), performance and scalability can be improved immensely ! E.g. if a HTTP listener thread allocated to listen to client requests is released immediately, then it is free to attend to requests from other clients and the business logic can be executed in a separate container thread which can then return the response via appropriate methods such as java.util.concurrent.Future object or via call back handlers registered by the client. Think from an end user perspective – responsiveness matters a lot! Dive In: Async constructs and APIs in Java EE Let’s take a look at few of the Async related features (APIs) in Java EE. This is not an exhaustive list – but should be a good starting point. Different Java EE specs have their typical ways and APIs to facilitate Async capabilities. Let’s explore the below Java EE specificationsJAX-RS 2.0 (Java EE 7) Websocket 1.0 (Java EE 7) Concurrency Utilities 1.0 (Java EE 7) EJB 3.1 (Java EE 6) Servlet 3.0 (Java EE 6)Note: The code presented below is in a snippet form (for obvious reasons). The complete samples can be accessed here.  JAX-RS 2.0 Async processing of requests is a new feature in 2.0 edition of JAX-RS (new in Java EE 7). In order to execute an aysnc request using JAX-RS APIs, one needs to inject a reference to a javax.ws.rs.container.AsyncResponse interface in the JAX-RS resource method itself. This parameter puts the request execution in async mode and the method proceeds with its execution. The resume method on the AsynResponse object needs to be called from within a separate thread after the business logic execution is complete. One can leverage the Java EE concurrency utility features (discussed later) such as the javax.enterprise.concurrent.ManagedExecutorService in order to encapsulate the business logic as a Runnable object and submitting it to the container’s executor service which takes care of the rest. No need to spawn un-managed, isolated threads on your own. @Path("/{id}") @GET @Produces("application/xml") public void asyncMethod(@Suspended AsyncResponse resp, @PathParam("id") String input) {System.out.println("Entered MyAsyncRESTResource/asyncMethod() executing in thread: "+ Thread.currentThread().getName()); mes.execute( () -> { System.out.println("Entered Async zone executing in thread: "+ Thread.currentThread().getName()); System.out.println("Simulating long running op via Thread sleep() started on "+ new Date().toString()); try { Thread.sleep(5000); } catch (InterruptedException ex) { Logger.getLogger(MyAsyncRESTResource.class.getName()).log(Level.SEVERE, null, ex); } System.out.println("Completed Long running op on "+new Date().toString()); System.out.println("Exiting Async zone executing in thread: "+ Thread.currentThread().getName());//creating a dummy instance of our model class (Student)Student stud = new Student(input, "Abhishek", "Apr-08-1987"); resp.resume(Response.ok(stud).build()); } );System.out.println("Exit MyAsyncRESTResource/asyncMethod() and returned thread "+Thread.currentThread().getName()+" back to thread pool"); } The JAX-RS Client API also has asynchronous capabilities but they have not been discussed in the post. They are definitely worth a look! Websocket 1.0 The Websocket API is a brand new addition to the Java EE arsenal (introduced in Java EE 7). It facilitates bi-directional (both server and client initiated) communication which is also full duplex in nature (either the client or server can send messages to each other at any time). In order to send async messages using the Websocket API, one needs to use the getAsyncRemote method available on the javax.websocket.Session interface. Internally, this is nothing but an instance of the nested interface of the javax.websocket.RemoteEnpoint – javax.websocket.RemoteEnpoint.Async. Calling the regular sendXXX methods on this would result in the sending process being executed in a separate thread. This is particularly useful when you consider exchange of large messages or handling large numbers of websocket clients to whom the messages need to be sent. The method wither returns a java.util.concurrent.Future object or one can register a callback in the form of a javax.websocket.SendHandler interface implementation. public void sendMsg(@Observes Stock stock) { System.out.println("Message receieved by MessageObserver --> "+ stock); System.out.println("peers.size() --> "+ peers.size()); peers.stream().forEach((aPeer) -> { //stock.setPrice();aPeer.getAsyncRemote().sendText(stock.toString(), (result) -> { System.out.println("Message Sent? " + result.isOK()); System.out.println("Thread : " + Thread.currentThread().getName()); });}); } Concurrency Utilities 1.0 The Java EE Concurrency Utilities is another great addition to Java EE 7. It provides a standard way of spawning threads – the good part is that these are container managed and not just isolated/orphan threads about which the container has no contextual information. In general, the Concurrency Utilities 1.0 provide a few standard constructs for execution of asynchronous tasks in separate threads. These are as follows – javax.enterprise.concurrent.ManagedExecutorService  and javax.enterprise.concurrent.ManagedScheduledExecutorService. In order to start a new task in a separate thread, one can use the ManagedExecutorService interface to submit a Runnable. In addition to implementing the Runnable interface, a class can also implement the javax.enterprise.concurrent.ManagedTask interface and provide a  javax.enterprise.concurrent.ManagedTaskListener implementation in order to listen to life  cycle changes to the task submitted via the ManagedExecutorService. @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {System.out.println("Enter AConcurrencyUtilsExample/doGet executing in thread "+ Thread.currentThread().getName()); System.out.println("initiating task . . . "); mes.execute(new AManagedTask()); System.out.println("Exit AConcurrencyUtilsExample/doGet and returning thread "+ Thread.currentThread().getName() +" back to pool"); } Servlet 3.0 Asynchronous HTTP was introduced in Servlet 3.0 (part of Java EE 6) which basically provided the capability to execute the request in separate thread and suspend the original thread which handled the client invocation. The key player here is the javax.servlet.AsyncContext interface. In order to initiate asynchronous processing, the startAsync method of the java.servlet.ServletRequest interface is called. In order to execute the core logic, a java.lang.Runnable object needs to be submitted to the start method of the AsyncContext interface. One can choose to attach a listener by implementing the javax.servlet.AsyncListener in order to receive callback notifications during specific times of the Async task execution. @Overridepublic void doGet(HttpServletRequest req, HttpServletResponse resp) {PrintWriter writer = null; try { writer = resp.getWriter(); } catch (IOException ex) { Logger.getLogger(MyAsyncServlet.class.getName()).log(Level.SEVERE, null, ex); } //System.out.println("entered doGet()"); writer.println("ENTERING ... " + MyAsyncServlet.class.getSimpleName() + "/doGet()"); writer.println("Executing in Thread: " + Thread.currentThread().getName()); //step 1 final AsyncContext asyncContext = req.startAsync();//step 2 asyncContext.addListener(new CustomAsyncHandler(asyncContext));//step 3 asyncContext.start( () -> { PrintWriter logger = null; try { logger = asyncContext.getResponse().getWriter(); } catch (IOException ex) { Logger.getLogger(MyAsyncServlet.class.getName()).log(Level.SEVERE, null, ex); }logger.println("Long running Aync task execution started : " + new Date().toString());logger.println("Executing in Thread: " + Thread.currentThread().getName()); try { Thread.sleep(5000); } catch (InterruptedException e) { Logger.getLogger(MyAsyncServlet.class.getName()).log(Level.SEVERE, null, e); }logger.println("Long task execution complete : " + new Date().toString());logger.println("Calling complete() on AsyncContext");//step 4 asyncContext.complete(); } );writer.println("EXITING ... " + MyAsyncServlet.class.getSimpleName() + "/doGet() and returning initial thread back to the thread pool");} EJB 3.1 Typically, (prior to EJB 3.1) EJB Message Driven Beans were used to fulfill async related requirements. A MDB bean listens to messages sent to a javax.jms.Destination (a Queue or Topic) and executes the required business logic – this might be anything from sending an email to initiating an order processing task. The important thing to understand is that the client which sends the message to the Queue in the first place is unaware of the MDB (decoupled) and does not have to wait/remain blocked until the end of the job (email receipt or order processing confirmation). EJB 3.1 (part of Java EE 6) introduced the javax.ejb.Asynchronous annotation. This can be placed on a EJB Session bean (Stateless, Stateful or Singleton) class (makes all the methods Asynchronous) or at the method level itself – in case fine grained control is needed. A method with the @Asynchronous annotation can either return void (fire and forget) or an instance of java.util.concurrent.Future if the async method result needs to be tracked – this can be done by calling the Future.get() – the thing to note is that the get method itself is blocking in nature. @Asynchronous public Future<String> asyncEJB2(){System.out.println("Entered MyAsyncEJB/asyncEJB2()"); System.out.println("MyAsyncEJB/asyncEJB2() Executing in thread: "+ Thread.currentThread().getName()); System.out.println("Pretending as if MyAsyncEJB/asyncEJB2() is doing something !"); try { Thread.sleep(5000); } catch (InterruptedException ex) { java.util.logging.Logger.getLogger(MyAsyncEJB.class.getName()).log(Level.SEVERE, null, ex); }System.out.println("Exiting MyAsyncEJB/asyncEJB2()"); return new AsyncResult("Finished Executing on "+ new Date().toString());} This was a rather brief preview of Java EE capabilities. These APIs and specifications are functionally rich and it is hard cover all of them via a blog post! I hope this piques your interest and gives you a starting point to explore further. Cheers!Reference: Java EE: Asynchronous constructs and capabilities from our JCG partner Abhishek Gupta at the Object Oriented.. blog....

HawtIO on JBoss EAP (Part II)

I just posted an entry on running HawtIO on JBoss Wildfly 8.1 recently. From that post you know how awesome HawtIO is, and all of the cool plugins it has to make managing your JVM based technologies from a single dashboard… well… hawt….However, I wanted to dedicate a post to running HawtIO on JBoss EAP for the same reasons outlined in the previous post: As we at Red Hat roll out JBoss Fuse and JBoss Fuse Serviceworks, we’re getting better at integrating the individual components. For example, a Fuse Service Works subscription gives you full access to A-MQ, Fuse, and all of its components, including HawtIO. Unfortunately, HawtIO isn’t “officially” supported in EAP as of today, but that will be fixed in next releases. It’s not a limitation of the technology, it’s just there’s so much there and Red Hat has stringent testing/compatibility requirements so we need to have all of the testing/certification done before we “support it” fully. BUT… there’s really no reason to not use it anyway (at least Development and QA), while we wait for support. And there are lots of people already doing that. Just remember, it’s not officially supported yet! So the rest of this blog is a step-by-step guide with best practices for getting HawtIO deployed and secured on your JBoss EAP 6.x application server. I will use HawtIO 1.4.11 (latest release from the community) for this guide.HawtIO on JBoss EAP 6.1 This first section will discuss EAP 6.1 because it seems to work great following the similar directions as those for deploying HawtIO on Wildfly 8.1. Basically, if you follow the instructions from the blog post on running on Wildfly, you’ll get it working just fine. To be very specific do these pieces:Remove the log4j.properties file to use EAP logging subsystem Rename distro to hawtio.war Create users in ApplicationRealm with roles including “admin” Edit the standalone.xml file to account for the following settings:I’ve also added [these instructions to the HawtIO documentation][http://hawt.io/getstarted/index.html] (the website should update with those changes soon). After successfully deploying HawtIO to JBoss EAP 6.1, you should be greeted with a login page at this URL: http://localhost:8080/hawtioHawtIO on JBoss EAP 6.2 So unfortunately, getting HawtIO to run on JBoss EAP 6.2 has some issues with JAAS that I haven’t been able to figure out. If you’re a Wildfly 7/EAP 6.x wizard and wish to take a look have a peek at the github issue I have open. Thankfully, however, HawtIO is quite versatile and doesn’t have to be run as a web-app collocated on the container you want to manage. You can run it standalone as a Chrome extension or in a separate container that’s not JBoss EAP. Effectively, you can use HawtIO’s Connect page to remotely connect to the container you wish to manage. For example, download the standalone distribution of HawtIO which ends up just being an executable jar. Run it like this (make sure to add the params for the port change, because we don’t want to conflict with the EAP ports if running on the same machine): ceposta@postamachat(standalone) $ java -jar hawtio-app-1.4.11.jar --port 9091 This will bring up HawtIO running standalone (with an internal Jetty server! Woohoo!). Then click the “Connect” tab and you should see a page like this:Wait, what? Just connect? “How would this even work”” you ask? “How can we connect to EAP and have HawtIO just manage it?” Well, the magic is this totally awesome little library named Jolokia.Jolokia provides REST access to your JMX tree. So the way to get HawtIO to manage your JBoss EAP 6.2 container is to enable Jolokia on it and then use HawtIO’s Connect tab to log in. If you haven’t taken a look at Jolokia, you’re missing out!! No more complicated JMX API/clients and query/filter API. Just use REST endpoints and curl. Simple. Jolokia has a few options for deploying/enabling. The most straight forward is a web app, but you could also attach as a JVM agent at startup time. We’ll take the easy approach and deploy Jolokia as a webapp: Rename distro For this blog post, I’m using Jolokia 1.2.2. So when you download it, it has a name like this jolokia-1.2.2.war. Let’s rename the distro so we don’t take into account the version numbers to make it easier to get to the URL: ceposta@postamachat(rename) $ mv jolokia-war-1.2.2.war jolokia.war Enable security for Jolokia Next, we want to enable security for the jolokia web app. We’ll do that as described in the Jolokia documentation. We’ll need to edit the web.xml file that comes in the Jolokia jar and uncomment the section that’s blocked out for security: ceposta@postamachat(rename) $ vi jolokia.war Your config in WEB-INF/web.xml should have a security section like this: 163 <!-- 164 Example Configuration for switching on BASIC security. The role 'Jolokia' 165 must be mapped to real users with passwords on the Appserver side: 166 --> 167 <login-config> 168 <auth-method>BASIC</auth-method> 169 <realm-name>Jolokia</realm-name> 170 </login-config> 171 172 <security-constraint> 173 <web-resource-collection> 174 <web-resource-name>Jolokia-Agent Access</web-resource-name> 175 <url-pattern>/*</url-pattern> 176 </web-resource-collection> 177 <auth-constraint> 178 <role-name>Jolokia</role-name> 179 </auth-constraint> 180 </security-constraint> 181 182 <security-role> 183 <role-name>Jolokia</role-name> 184 </security-role> 185 186 </web-app> Now add a user like we did in the previous blog that has a role of “Jolokia”. Note, make sure this user is added to the ApplicationRealm. Connect to EAP 6.2 Now go back to your standalone HawtIO (we deployed it in the previous step at http://localhost:9091/hawtio) and enter the connect params (path as follows):Click “Connect to remote server” and viola! You’re conencted. Where to go from here You can also try to enable SSL over the http connection as well as maybe use DIGEST auth instead of the default BASIC auth. I’ll leave that as an exercise for the reader!Reference: HawtIO on JBoss EAP (Part II) from our JCG partner Christian Posta at the Christian Posta – Software Blog blog....
Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below: