Featured FREE Whitepapers

What's New Here?


Tips for (unit testing) JavaBeans

If you’re writing Java code chances are you’re writing at least a few classes that adhere to the JavaBean conventions, i.e., classes that have private properties with public getter and setter methods, contain a no-arguments constructor, are serializable, and comply with the Equals and HashCode contract. And on top of that you’ll probably also throw in a useful toString() implementation. If, e.g., we take a very simple class called MyBean that contains two fields named id and name, we’ll end up with the following code: MyBean – a JavaBean example   package it.jdev.example;import java.io.Serializable;public class MyBean implements Serializable {private static final long serialVersionUID = 6170536066049208199L;private long id; private String name;public MyBean() { super(); }public long getId() { return id; }public void setId(final long id) { this.id = id; }public String getName() { return name; }public void setName(final String name) { this.name = name; }@Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + (int) (id ^ (id >>> 32)); result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; }@Override public boolean equals(final Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final MyBean other = (MyBean) obj; if (id != other.id) { return false; } if (name == null) { if (other.name != null) { return false; } } else if (!name.equals(other.name)) { return false; } return true; }@Override public String toString() { return "MyBean [id=" + id + ", name=" + name + "]"; }} So for a class with just two fields, we’ve ended up with 70 lines of code. That is a lot of boilerplate code. And furthermore, every time you add or change any properties you will have to adjust or regenerate alot of that boilerplate code. Project Lombok to the rescue Luckily, there is a nice open source tool that aims to reduce just the type of boilerplate code we’re dealing with in our MyBean class. It is called Project Lombok. Just install Lombok as a plugin in your favourite IDE, and include the Lombok jar file on your build classpath or add it as a maven dependency, and you should be good to go. Project Lombok contains alot of different annotations but for our example we’re only going to need one: @Data. When we apply the annotation to our code we’re down to a mere 15 lines of code from our original 70 lines, with Project Lombok generating all the methods for us during compilation. And moreover, we never again have to worry about our hashCode(), equals(), and toString() methods running out-of-sync. MyBean – our JavaBean example with Project Lombok package it.jdev.example;import java.io.Serializable;import lombok.Data;@Data public class MyBean implements Serializable {private static final long serialVersionUID = 6170536066049208199L;private long id; private String name;} Help, my code coverage is down The fact that we now have Project Lombok generating the boilerplate code for us, doesn’t necessarily mean that we can skip unit testing the generated methods. Especially if you value code coverage, and you have minimal coverage level checks in place in your CI set-up, you’ll want to add some extra tests. Luckily, there are some easy ways to get your code coverage up. Testing Serializability If your serializable objects contain any custom fields, these probably should also be serializable. However, this is something that is easily overlooked. Using the SerializationUtils class from the Apache Commons Lang library, you can write a very simple test that checks whether an object correctly serializes, and deserializes back again. Testing for Serializibility of our MyBean package it.jdev.example;import static org.junit.Assert.*;import org.apache.commons.lang3.SerializationUtils; import org.junit.Before; import org.junit.Test;public class MyBeanTest {private MyBean myBean;@Before public void setUp() throws Exception { myBean = new MyBean(); myBean.setId(123L); myBean.setName("Bean, James Bean"); }@Test public void beanIsSerializable() { final byte[] serializedMyBean = SerializationUtils.serialize(myBean); final MyBean deserializedMyBean = (MyBean) SerializationUtils.deserialize(serializedMyBean); assertEquals(myBean, deserializedMyBean); }} Testing getter and setters methods Testing the getter and setter method pairs of a JavaBean can become very tedious very quickly. Fortunately there is a nice test library called meanBean that can do the work for us. So after adding the following method to our unit test, we’re finished testing the getters and setters: Testing the getters and setters of our MyBean example @Test public void getterAndSetterCorrectness() throws Exception { new BeanTester().testBean(MyBean.class); } Testing equals() and hashCode() Testing all the intricacies of the equals and hashCode contract yourself is a very wearisome task. Again, there are some nice tools that can take it off your hands. The aforementioned library meanBean offers functionality to do so. However, I found a tool like EqualsVerifier to be a tad more stringent in its tests, and it also provides detailled explaination about any errors. So we’re going to add the next test case to our suite: Testing the Equals and HashCode contract of our MyBean example @Test public void equalsAndHashCodeContract() throws Exception { EqualsVerifier.forClass(MyBean.class).suppress(Warning.STRICT_INHERITANCE, Warning.NONFINAL_FIELDS).verify(); } Note, that we are suppressing some warnings here. For more information as to why, see the EqualsVerifier information about errormessages: http://www.jqno.nl/equalsverifier/errormessages/. A common base class for our JavaBean test cases Even with tools like meanBean and EqualsVerifier doing the heavy lifting, you don’t want to repeat the same test code over and over again. So you’ll probably will want to put the tests in an abstract base class. A possible implementation of that base class could look something like the following: An abstract base class for testing JavaBeans package it.jdev.example;import static org.junit.Assert.assertEquals; import java.io.Serializable; import java.time.LocalDateTime; import nl.jqno.equalsverifier.EqualsVerifier; import nl.jqno.equalsverifier.Warning; import org.apache.commons.lang3.SerializationUtils; import org.junit.Test; import org.meanbean.lang.Factory; import org.meanbean.test.BeanTester;public abstract class AbstractJavaBeanTest {protected String[] propertiesToBeIgnored;@Test public void beanIsSerializable() throws Exception { final T myBean = getBeanInstance(); final byte[] serializedMyBean = SerializationUtils.serialize((Serializable) myBean); @SuppressWarnings("unchecked") final T deserializedMyBean = (T) SerializationUtils.deserialize(serializedMyBean); assertEquals(myBean, deserializedMyBean); }@Test public void equalsAndHashCodeContract() { EqualsVerifier.forClass(getBeanInstance().getClass()).suppress(Warning.STRICT_INHERITANCE, Warning.NONFINAL_FIELDS).verify(); }@Test public void getterAndSetterCorrectness() throws Exception { final BeanTester beanTester = new BeanTester(); beanTester.getFactoryCollection().addFactory(LocalDateTime.class, new LocalDateTimeFactory()); beanTester.testBean(getBeanInstance().getClass()); }protected abstract T getBeanInstance();/** * Concrete Factory that creates a LocalDateTime. */ class LocalDateTimeFactory implements Factory {@Override public LocalDateTime create() { return LocalDateTime.now(); }}} Note that – just for the fun of it – I’ve added a LocalDateTimeFactory so that meanBean can test the getters and setters of any LocalDateTime attributes you might have used in your JavaBean class. Applying the abstract base class to the unit test of our MyBean example, the resulting unit test would be something like: The final unit test for our MyBean package it.jdev.example;import static org.junit.Assert.*;import org.junit.Test;public class MyBeanTest extends AbstractJavaBeanTest<MyBean> {@Override protected MyBean getBeanInstance() { return new MyBean(); }}Reference: Tips for (unit testing) JavaBeans from our JCG partner Wim van Haaren at the JDev blog....

Eliminating Development Redeploys using Gradle

For service development, my team recently moved away from Grails to the Dropwizard framework. One of the things I really missed from the Grails stack, though, was auto-reloading: any changes to source files appear in the running app moments after saving, without a restart. It proved feasible to pull this functionality into Gradle builds as well. Spring Loaded is the library that Grails uses under its hood. It supports reloading quite a few types of changes without restarting the JVM:      Add/modify/delete methods/fields/constructors Change annotations on types/methods/fields/constructors Add/remove/change values in enum typesThe other piece I needed was a watch plugin: something to trigger Gradle tasks when source files change. For the full working example, clone my demo Github repository. The first piece of setup is adding an additional configuration. This isolates the spring-loaded.jar (which is only needed during development) from the standard configurations such as compile: configurations { agent }The dependency block reads as follows: configurations { dependencies { compile 'org.codehaus.groovy:groovy-all:2.3.4' compile 'io.dropwizard:dropwizard-core:0.7.1' compile 'com.sun.jersey:jersey-client:1.18' agent "org.springframework:springloaded:${springloadedVersion}" } The compile dependencies are the standard set one would expect in a Dropwizard project. The line starting with “agent” adds the Spring Loaded dependency to the agent configuration defined earlier. The build script uses this dependency to get the spring-loaded.jar onto the file system. springloadedVersion is a constant defined earlier in the build file. task copyAgent(type: Copy) { from configurations.agent into "$buildDir/agent" } run.mustRunAfter copyAgentThe above copyAgent task will take the spring-loaded.jar file and copy it to the build directory for later use as a javaagent. run is also configured to follow copyAgent in the chain. buildscript { repositories { jcenter() } dependencies { classpath 'com.bluepapa32:gradle-watch-plugin:0.1.3' } }apply plugin: 'watch' watch { groovy { files files('src/main/groovy') tasks 'compileGroovy' } }task watchThread() << { Thread.start { project.tasks.watch.execute() } } run.mustRunAfter watchThreadThe above script block adds and configures watch. The buildscript block adds the proper repository and the the watch plugin as a dependency. The watch block configures the plugin; whenever there are changes in src/main/groovy, the Groovy source will be recompiled. The watchThread task executes watch parallely. This is needed because the final job will execute two tasks which both run continuously: watch and run. watch would normally block run. Finally, the run task is configured to follow watchThread when both are part of the chain. run { args = ['server', 'app.yaml'] jvmArgs = ["-javaagent:${new File("$buildDir/agent/springloaded-${springloadedVersion}.jar").absolutePath}", '-noverify'] }task reloading(dependsOn: [watchThread, copyAgent, run])This final bit of code configures the run command with a javaagent flag. This tells the JVM to use Spring Loaded and let it do its magic. Spring Loaded also needs the noverifyflag. The reloading task is the actual task to run during development. It strings the tasks to copy the agent, spin up a thread watching for source changes, and running Dropwizard’s main method. This configuration structure would also support frameworks outside of Dropwizard: anything with a main method, really. Though it doesn’t work for all kinds of code changes, it can eliminate a great many application restarts during development.Reference: Eliminating Development Redeploys using Gradle from our JCG partner Matt Cholick at the Cholick.com blog....

Android promoted Actions: Floating action button (FAB)

Recently new design pattern were introduced in Android. One of the most known pattern is the Promoted Actions. They are actions that  are directly visible in the UI instead of using action bar button; for this reason these actions are called promoted, they can be easily accessed and define the main action in the current UI. For example, if we are using an email app and we are listing the inbox folder, one promoted action can be a new mail. The visual pattern is called Float Action Button (or FAB) because the promoted action can be represented using a floating circular button on the UI. This type of design patterns are available in Android L but we can create a floating button also in previous Android version. Let us suppose we have a list of elements in our UI, the promoted action can be ‘Add new item’, so we can have something like the pic shown below:There are several way we can use to crate a floating action button. Floating action button using ImageButton The first way to create a FAB is using the ImageButton, we simply add the ImageButton to our UI layout and exploit some new features provided by Android L. <ImageButton android:layout_width="56dp" android:layout_height="56dp" android:src="@drawable/plus" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_marginRight="16dp" android:layout_marginBottom="16dp" android:tint="@android:color/white" android:id="@+id/fab" android:elevation="1dp" android:background="@drawable/ripple" android:stateListAnimator="@anim/fab_anim" /> If we look above, we can notice it is a normal ImageButton that we place usually at lower right corner. There are some aspects we should consider:Background Shadow AnimationTo make our button more “attractive”, we can implement some effect as background. If we notice we use a ripple drawable defined in this way: <ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="?android:colorControlHighlight"> <item> <shape android:shape="oval"> <solid android:color="?android:colorAccent" /> </shape> </item> </ripple> We said it is a floating button, so it means it should float above the UI data as if it was in another plane level. This effect can be obtained using the elevation attribute that “moves”  our UI component on Z-axis. The last thing is the animation effect as we press the button. We want to give to the user the feeling the button is sinking as we press it, so we have to enlarge the shadow around it: <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_enabled="true" android:state_pressed="true"> <objectAnimator android:duration="@android:integer/config_shortAnimTime" android:propertyName="translationZ" android:valueFrom="@dimen/start_z" android:valueTo="@dimen/end_z" android:valueType="floatType" /> </item> <item> <objectAnimator android:duration="@android:integer/config_shortAnimTime" android:propertyName="translationZ" android:valueFrom="@dimen/end_z" android:valueTo="@dimen/start_z" android:valueType="floatType" /> </item> </selector> Floating Action Button using a Custom Component Another way to create our Floating Button is using a custom component. In this case we don’t use the Android L feature but we code it manually. As first thing, we create our class that represents the custom component: public class CustomFAB extends ImageButton { ... } The next step is reading some custom attributes that affect the UI behavior, we can do it in the method called init: private void init(AttributeSet attrSet) { Resources.Theme theme = ctx.getTheme(); TypedArray arr = theme.obtainStyledAttributes(attrSet, R.styleable.FAB, 0, 0); try { setBgColor(arr.getColor(R.styleable.FAB_bg_color, Color.BLUE)); setBgColorPressed(arr.getColor(R.styleable.FAB_bg_color_pressed, Color.GRAY)); StateListDrawable sld = new StateListDrawable();sld.addState(new int[] {android.R.attr.state_pressed}, createButton(bgColorPressed)); sld.addState(new int[] {}, createButton(bgColor)); setBackground(sld); }catch(Throwable t) {} finally { arr.recycle(); }} At the same time we define these attributes in a XML file: <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="FAB"> <!-- Background color --> <attr name="bg_color" format="color|reference"/> <attr name="bg_color_pressed" format="color|reference"/> </declare-styleable> </resources> If you notice we have some attribute that control the background color. At line 8 we define several button states using StateListDrawable and for each state we create our button: private Drawable createButton(int color) { OvalShape oShape = new OvalShape(); ShapeDrawable sd = new ShapeDrawable(oShape); setWillNotDraw(false); sd.getPaint().setColor(color);OvalShape oShape1 = new OvalShape(); ShapeDrawable sd1 = new ShapeDrawable(oShape);sd1.setShaderFactory(new ShapeDrawable.ShaderFactory() { @Override public Shader resize(int width, int height) { LinearGradient lg = new LinearGradient(0,0,0, height, new int[] { Color.WHITE, Color.GRAY, Color.DKGRAY, Color.BLACK }, null, Shader.TileMode.REPEAT);return lg; } });LayerDrawable ld = new LayerDrawable(new Drawable[] { sd1, sd }); ld.setLayerInset(0, 5, 5, 0, 0); ld.setLayerInset(1, 0, 0, 5, 5);return ld; } Finally we can add this component to our Layout: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:custom="http://schemas.android.com/apk/res/com.survivingwithandroid.fab" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MyActivity"> ...<com.survivingwithandroid.fab.CustomFAB android:layout_width="56dp" android:layout_height="56dp" android:src="@android:drawable/ic_input_add" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_marginRight="16dp" android:layout_marginBottom="16dp" custom:bg_color="@color/light_blue" android:tint="@android:color/white" /> </RelativeLayout>Source code available @ githubReference: Android promoted Actions: Floating action button (FAB) from our JCG partner Francesco Azzola at the Surviving w/ Android blog....

One Jar To Rule Them All: Apache TomEE + Shrinkwrap == JavaEE Boot

WARNING: I am not an expert of Spring Boot. There are a lot of things that I find really interesting about it and of course that can really improve your day-to-day work. Moreover I don’t have anything against Spring Boot nor people who develop it or use it. But I think that community are overestimating/overvenerating this product. A year ago I started to receive a lot links about blogposts, tweets, information about Spring Boot. From his website you can read:   Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that can you can “just run”.  And it seems that this thing has just revolutionized the Java world. For example a Spring MVC (RESTful as well) application in Spring Boot looks like: @Controller @EnableAutoConfiguration public class SampleController {@RequestMapping("/") @ResponseBody String home() { return "Hello World!"; }public static void main(String[] args) throws Exception { SpringApplication.run(SampleController.class, args); } } As you can see the magic happens inside SpringApplication class which starts an embedded Tomcat or Jetty and using Spring thing it registers this controller. Pretty impressive I know with a few lines you can have an endpoint ready to be used. But I wonder to myself if it is possible to use the same approach in JavaEE world and with the same low-level and light requirements. And the answer is absolutely. I have just created a really small protoype/proof-of-concept to prove that it is possible. Also please don’t misunderstand me, Spring boot offers a lot of more things apart from self-contained application like monitoring, actuators, or artifact dependency resolution. But these things are only integration with other technologies, my example has been developed from zero in 1 hour and a half so don’t expect to have a Spring boot ready to be used. The first thing to choose is the application server to be used, and in this case there is no doubt that the best one for this task is Apache TomEE. It is a certified web profile Java EE server which takes 1 second to start up and works with default Java memory parameters. So I added tomee dependencies in my pom.xml file. <dependencies><dependency> <groupId>org.apache.openejb</groupId> <artifactId>tomee-embedded</artifactId> <version>1.7.1</version> </dependency><dependency> <groupId>org.apache.openejb</groupId> <artifactId>openejb-cxf-rs</artifactId> <version>4.7.1</version> </dependency> <dependency> <groupId>org.apache.openejb</groupId> <artifactId>tomee-jaxrs</artifactId> <version>1.7.1</version> </dependency> <dependency> <groupId>org.jboss.shrinkwrap</groupId> <artifactId>shrinkwrap-depchain</artifactId> <version>1.2.2</version> <type>pom</type> </dependency></dependencies> In used tomee embedded version (1.7.1) you can only deploy applications contained inside a file, you cannot add for example a Servlet programmatically like it is done in Tomcat. This may change in near future of embedded tomee API, but for now we are going to use ShrinkWrap to create these deployment files in a programmatic way. This is what we want to do: import javax.ejb.Stateless; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces;@Stateless @Path("/sample") public class SampleController {@GET @Produces("text/plain") public String sample() { return "Hello World"; }public static void main(String args[]) { TomEEApplication.run(HelloWorldServlet.class, SampleController.class); } } Notice that we are only importing JavaEE classes and it is as reduced as Spring Boot one. In only 2 seconds the application is ready to be used. Keep in mind that you can use any feature provided by web profile spec as well as JAX-RS or JMS. So for example you can use JPA, Bean Validation, EJBs, CDI, … And what’s inside TomEEApplication? You will be surprised a class with only 70 lines: public class TomEEApplication {private static void startAndDeploy(Archive archive) {Container container;try { Configuration configuration = new Configuration(); String tomeeDir = Files.createTempDirectory("apache-tomee").toFile().getAbsolutePath(); configuration.setDir(tomeeDir); configuration.setHttpPort(8080);container = new Container(); container.setup(configuration);final File app = new File(Files.createTempDirectory("app").toFile().getAbsolutePath()); app.deleteOnExit();File target = new File(app, "app.war"); archive.as(ZipExporter.class).exportTo(target, true); container.start();container.deploy("app", target); container.await();} catch (Exception e) { throw new IllegalArgumentException(e); }registerShutdownHook(container);}private static void registerShutdownHook(final Container container) { Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { try { if(container != null) { container.stop(); } } catch (final Exception e) { throw new IllegalArgumentException(e); } } }); }public static void run(Class ... clazzes) { run(ShrinkWrap.create(WebArchive.class).addClasses(clazzes)); }public static void run(WebArchive archive) { startAndDeploy(archive); } } As you may see it is really simple piece of code and for example configuration or name of the application is hardcoded, but see that with several small easy changes you may start configuring server, application and so on. In summary, of course Spring Boot is cool, but with really simple steps you can start having the same in JavaEE world. We (Apache TomEE contributors) are going to start to work on this and expand this idea. So don’t underestimate Java EE because of Spring Boot.Reference: One Jar To Rule Them All: Apache TomEE + Shrinkwrap == JavaEE Boot from our JCG partner Alex Soto at the One Jar To Rule Them All blog....

Java EE + MongoDb with Apache TomEE and Jongo Starter Project

Know MongoDB and Java EE, but you don’t know exactly how to integrate both of them? Do you read a lot about the topic but you have not found a solution which fits this purpose? This starter project is for you: You will learn how to use MongoDB and Java EE in a fashion way without having to depend on Spring Data MongoDB framework but with “similar” basic features. The only thing better than a Maven archetype is a repository you can fork with everything already setup. Skip the documentation and just fork-and-code. This starter project contains:  Jongo as MongoDB Mapper (www.jongo.org). Apache TomEE as application service and integration. (tomee.apache.org) Arquillian for tests. (www.arquillian.org)The example is pretty simple, we want to store colors inside a MongoDB collection. Our POJO is like: public class Color {@ObjectId private String _id; private String name; private int r; private int g; private int b; public Color() { super(); }public Color(String name, int r, int g, int b) { super(); this.name = name; this.r = r; this.g = g; this.b = b; }// getters and setters } Note that we are using @ObjectId annotation provided by Jongo to set this field as MongoDB id. Moreover because it is called _id, the id will be set automatically. Then the service layer: @Singleton @Lock(LockType.READ) public abstract class ColorService implements InvocationHandler {@JongoCollection("color") @Inject MongoCollection colorMongoCollection; @Insert public abstract Color createColor(Color c); @Remove public abstract int removeAllColors(); @FindById public abstract Color findColorById(String id); @FindOne("{name:#}") public abstract Color findColorByColorName(String colorName); @Find("{r:#}") public abstract Iterable<Color> findColorByRed(int r); public long countColors() { return colorMongoCollection.count(); } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return PersistenceHandler.invoke(colorMongoCollection, method, args); } } Note that there isn’t a lot of code, but some points are really interesting stuff. Let’s analyze them. @Singleton is used to define an EJB as singleton, it works with @Stateless as well, for Java EE users no news here. The class is abstract. Why? Because it allows us to not implement all methods but define them. Also implements java.lang.reflect.InvocationHandler. This is because we want to use one really interesting feature which allows us to create a fallback method called invoke. For any method defined but not implemented this method is called. We have a MongoCollection class (from Jongo project) that it is injected. MongoCollection represents a collection in MongoDB. Because we need to set which collection we want to work with, an annotation called @JongoCollection is created so you can pass the name of the backend collection. Note that MongoCollection is produced by CDI container by using our custom producer. Again no news here for CDI users. @ApplicationScoped public class MongoCollectionProducer {@Inject DB mongoDb; Jongo jongo;@PostConstruct public void initialize() throws UnknownHostException { jongo = new Jongo(mongoDb); }@Produces @JongoCollection MongoCollection collection(InjectionPoint injectionPoint) {JongoCollection jongoCollectionAnnotation = Reflection.annotation(injectionPoint .getQualifiers(), JongoCollection.class);if(jongoCollectionAnnotation != null) { String collectionName = jongoCollectionAnnotation.value(); return jongo.getCollection(collectionName); }throw new IllegalArgumentException(); }} Then there is a lot of methods which represents CRUD operations. Note that they are not implemented, they are only annotated with @Insert, @Find, @Remove, … to set which is the purpose of the method we want to execute. Some of them like finders or removers can receive Jongo-like query to be executed. And also a method called countColors which as you can see you can implement as custom method without relying to logic implemented within invoke method. And finally the invoke method. This method will be called for all abstract methods, and simply sends to PersistenceHandler class, which in fact is a util class against Jongo to execute the required operation. And that’s all, quite simple, and if you want to add new abstract operations, you only need to implement them inside PersistenceHandler class. Some of you may wonder why I use annotations and not the typical Spring Data approach where the name of the method indicates the operation. You can implement this approach as well, it is a simple matter of creating a regular expression inside PersistenceHandler class instead of if/else with annotations, but I prefer annotations approach. Faster in execution time, clean, and for example you can refactor the annotation name from @Find to @Buscar (Spanish equivalent) without worrying if you are breaking some regular expression. And finally the test: @RunWith(Arquillian.class) public class ColorTest {private static final String MONGODB_RESOURCE = "<resources>\n" + " <Resource id=\"mongoUri\" class-name=\"com.mongodb.MongoClientURI\" constructor=\"uri\">\n" + " uri mongodb://localhost/test\n" + " </Resource>\n" + "</resources>"; @Deployment public static JavaArchive createDeployment() { JavaArchive javaArchive = ShrinkWrap.create(JavaArchive.class) .addPackages(true, Color.class.getPackage()) .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml") .addAsManifestResource(new StringAsset(MONGODB_RESOURCE), "resources.xml") .merge(getJongoAndMongoDependecies()); return javaArchive; }private static JavaArchive getJongoAndMongoDependecies() { JavaArchive[] javaArchives = Maven.configureResolver() .loadPomFromFile("pom.xml") .resolve("org.mongodb:mongo-java-driver", "org.jongo:jongo") .withTransitivity() .as(JavaArchive.class);JavaArchive mergedLibraries = ShrinkWrap.create(JavaArchive.class);for (JavaArchive javaArchive : javaArchives) { mergedLibraries.merge(javaArchive); }return mergedLibraries; }@EJB ColorService colorService;@Before public void cleanDatabase() { colorService.removeAllColors(); }@Test public void should_insert_color() {Color color = colorService.createColor(new Color("red", 255, 0, 0));assertThat(color.getId(), notNullValue()); assertThat(color.getName(), is("red")); assertThat(color.getR(), is(255)); assertThat(color.getB(), is(0)); assertThat(color.getG(), is(0));}@Test public void should_count_number_of_colors() {colorService.createColor(new Color("red", 255, 0, 0)); colorService.createColor(new Color("blue", 0, 0, 255));assertThat(colorService.countColors(), is(2L));}@Test public void should_find_colors_by_id() {Color originalColor = colorService.createColor(new Color("red", 255, 0, 0));Color color = colorService.findColorById(originalColor.getId());assertThat(color.getId(), notNullValue()); assertThat(color.getName(), is("red")); assertThat(color.getR(), is(255)); assertThat(color.getB(), is(0)); assertThat(color.getG(), is(0));}@Test public void should_find_colors_by_name() {colorService.createColor(new Color("red", 255, 0, 0));Color color = colorService.findColorByColorName("red");assertThat(color.getId(), notNullValue()); assertThat(color.getName(), is("red")); assertThat(color.getR(), is(255)); assertThat(color.getB(), is(0)); assertThat(color.getG(), is(0));}@Test public void should_find_colors_by_red() {colorService.createColor(new Color("red", 255, 0, 0)); colorService.createColor(new Color("white", 255, 255, 255));Iterable<Color> colorByRed = colorService.findColorByRed(255);assertThat(colorByRed, hasItems(new Color("red", 255, 0, 0), new Color("white", 255, 255, 255)));}} This is an Arquillian test that has nothing special apart from one line: .addAsManifestResource(new StringAsset(MONGODB_RESOURCE), “resources.xml”) Because we are using Apache TomEE we use the way it has to configure elements to be used as javax.annotation.Resource in our code. The META-INF/resources.xml content will be: <resources> <Resource id="mongoUri" class-name="com.mongodb.MongoClientURI" constructor="uri"> uri mongodb://localhost/test </Resource> </resources> and then we use in our MongoClient producer to create the MongoClient instance to be used inside code. Note that we are using @Resource as any standard resource like DataSource, but in fact MongoClientURI is injected: @ApplicationScoped public class MongoDBProducer {@Resource(name = "mongoUri") private MongoClientURI mongoClientURI; private DB db;@PostConstruct public void init() throws UnknownHostException { MongoClient mongoClient = new MongoClient(mongoClientURI); db = mongoClient.getDB(mongoClientURI.getDatabase()); }@Produces public DB createDB() { return db; }} so in fact Mongo connection is configured in META-INF/resources.xml file and thanks of TomEE we can refer it as any standard resource. If you are going to use other application server you can change this approach to the one provided by it, or if you want you can use DeltaSpike extensions or your own method. Also because MongoClient database is get from a method annotated with @Produces  you can be injected it wherever you want on your code, so you can skip the abstract services layer if you want. What are the benefits of this approach? First that it is Java EE solution, you can use it without depending on Spring framework or any other library. You implement what you need, you do not download a bunch of libraries simply for accessing a MongoDB with some kind of object mapping. Also as you may see, the code is quite simple and there is no magic behind it, you can debug it without any problem, or even improve or change depending on your needs. The code is yours and is waiting to be modified. Do you want to use native MongoDB objects instead of Jongo? No problem, you can implement it. Moreover there aren’t much layers, in fact only one (the PersistenceHandler) so the solution is pretty fast in terms of execution. Of course this do not mean that you can’t use Spring Data MongoDB. It is a really interesting framework, so if you are already using Spring, go ahead with it, but if you plan to use a full J ava EE solution, then clone this project and start using MongoDB without having to make some research on the net about how to integrate both of them.You can clone the project from https://github.com/lordofthejars/tomee-mongodb-starter-projectReference: Java EE + MongoDb with Apache TomEE and Jongo Starter Project from our JCG partner Alex Soto at the One Jar To Rule Them All blog....

Built-in Fake Objects

While mock objects are perfect instruments for unit testing, mocking through mock frameworks may turn your unit tests into an unmaintainable mess. The root cause of this complexity is that our objects are too big. They have many methods and these methods return other objects, which also have methods. When we pass a mock version of such an object as a parameter, we should make sure that all of its methods return valid objects. This leads to inevitable complexity, which turns unit tests to waste almost impossible to maintain.   Object Hierarchy Take the Region interface from jcabi-dynamo as an example (this snippet and all others in this article are simplified, for the sake of brevity): public interface Region { Table table(String name); } Its table() method returns an instance of the Table interface, which has its own methods: public interface Table { Frame frame(); Item put(Attributes attrs); Region region(); } Interface Frame, returned by the frame() method, also has its own methods. And so on. In order to create a properly mocked instance of interface Region, one would normally create a dozen other mock objects. With Mockito it will look like this: public void testMe() { // many more lines here... Frame frame = Mockito.mock(Frame.class); Mockito.doReturn(...).when(frame).iterator(); Table table = Mockito.mock(Table.class); Mockito.doReturn(frame).when(table).frame(); Region region = Mockito.mock(Region.class); Mockito.doReturn(table).when(region).table(Mockito.anyString()); } And all of this is just a scaffolding before the actual testing. Sample Use Case Let’s say, you’re developing a project that uses jcabi-dynamo for managing data in DynamoDB. Your class may look similar to this: public class Employee { private final String name; private final Region region; public Employee(String empl, Region dynamo) { this.name = empl; this.region = dynamo; } public Integer salary() { return Integer.parseInt( this.region .table("employees") .frame() .where("name", this.name) .iterator() .next() .get("salary") .getN() ); } } You can imagine how difficult it will be to unit test this class, using Mockito, for example. First, we have to mock the Region interface. Then, we have to mock a Table interface and make sure it is returned by the table() method. Then, we have to mock a Frame interface, etc. The unit test will be much longer than the class itself. Besides that, its real purpose, which is to test the retrieval of an employee’s salary, will not be obvious to the reader. Moreover, when we need to test a similar method of a similar class, we will need to restart this mocking from scratch. Again, multiple lines of code, which will look very similar to what we have already written. Fake Classes The solution is to create fake classes and ship them together with real classes. This is what jcabi-dynamo is doing. Just look at its JavaDoc. There is a package called com.jcabi.dynamo.mock that contains only fake classes, suitable only for unit testing. Even though their sole purpose is to optimize unit testing, we ship them together with production code, in the same JAR package. This is what a test will look like, when a fake class MkRegion is used: public class EmployeeTest { public void canFetchSalaryFromDynamoDb() { Region region = new MkRegion( new H2Data().with( "employees", new String[] {"name"}, new String[] {"salary"} ) ); region.table("employees").put( new Attributes() .with("name", "Jeff") .with("salary", new AttributeValue().withN(50000)) ); Employee emp = new Employee("Jeff", region); assertThat(emp.salary(), equalTo(50000)) } } This test looks obvious to me. First, we create a fake DynamoDB region, which works on top of H2Data storage (in-memory H2 database). The storage will be ready for a single employees table with a hash key name and a single salary attribute. Then, we put a record into the table, with a hash Jeff and a salary 50000. Finally, we create an instance of class Employee and check how it fetches the salary from DynamoDB. I’m currently doing the same thing in almost every open source library I’m working with. I’m creating a collection of fake classes, that simplify testing inside the library and for its users. Related Posts You may also find these posts interesting:The Art of Software Testing by Glenford Myers CasperJS Tests in Maven Build XML/XPath Matchers for Hamcrest Bugs Are Welcome Phantomjs as an HTML ValidatorReference: Built-in Fake Objects from our JCG partner Yegor Bugayenko at the About Programming blog....

Embedding a XMPP server inside your JSF Web Application using Vysper, TomEE and PrimeFaces

I have an application that needs to notify the user when some job is done. It uses JSF and Primefaces, so this sort of notification could be implemented using atmosphere (aka Push). But another funny approach is to use a XMPP server embedded in your java web application. Ok, ok, you don’t have to embed it, you can just run an instance of a industrial-strenght XMPP server such as Openfire and Tigase. But hey, we’re just playing a little, so I’ll show you how to do it using Vysper, a little proof-of-concept developed using Apache Mina, that is simple enough for a few minutes play. Before showing how to do it, it’s nice to remember that threads and JEE applications usually don’t mix, so we can play, but we must be aware of what are we doing. First, you’re going to need these JARs, most of them come from Vysper. Just a few huh?aopalliance-1.0.jar commons-codec-1.4.jar commons-collections-3.1.jar commons-io-1.4.jar commons-lang-2.5.jar commons-logging-1.1.jar concurrent-1.3.4.jar derby- dnsjava-2.0.8.jar ehcache-core-2.2.0.jar fontbox-0.1.0.jar jackrabbit-api-1.5.0.jar jackrabbit-core-1.5.3.jar jackrabbit-jcr-commons-1.5.3.jar jackrabbit-spi-1.5.0.jar jackrabbit-spi-commons-1.5.0.jar jackrabbit-text-extractors-1.5.0.jar jcl-over-slf4j-1.5.3.jar jcr-1.0.jar jempbox-0.2.0.jar jetty-continuation-7.2.1.v20101111.jar jetty-http-7.2.1.v20101111.jar jetty-io-7.2.1.v20101111.jar jetty-security-7.2.1.v20101111.jar jetty-server-7.2.1.v20101111.jar jetty-servlet-7.2.1.v20101111.jar jetty-util-7.2.1.v20101111.jar jetty-websocket-7.2.1.v20101111.jar log4j-1.2.14.jar lucene-core-2.3.2.jar mina-core-2.0.2.jar nbxml-0.7.jar nekohtml-1.9.7.jar pdfbox-0.7.3.jar poi-3.0.2-FINAL.jar poi-scratchpad-3.0.2-FINAL.jar primefaces-4.0.jar servlet-api-2.5.jar slf4j-api-1.5.3.jar slf4j-log4j12-1.5.3.jar smack-3.1.0.jar smackx-3.1.0.jar spec-compliance-0.7.jar spring-aop-3.0.5.RELEASE.jar spring-asm-3.0.5.RELEASE.jar spring-beans-3.0.5.RELEASE.jar spring-context-3.0.5.RELEASE.jar spring-core-3.0.5.RELEASE.jar spring-expression-3.0.5.RELEASE.jar vysper-core-0.7.jar vysper-websockets-0.7.jar xep0045-muc-0.7.jar xep0060-pubsub-0.7.jar xep0124-xep0206-bosh-0.7.jar xercesImpl-2.8.1.jar xml-apis-1.3.03.jarNow, copy from Vysper the bogus certificate, so your XMPP server can “work” under a “secure” channel. It’s called bogus_mina_tls.cert. My xhtml looks like this: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:p="http://primefaces.org/ui"> <h:head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Messaging Prototype</title> <link rel="icon" type="image/png" href="favicon.ico"></link> </h:head> <h:body> <h:outputStylesheet library="css" name="style.css" /><p:ajaxStatus style="width:16px;height:16px;" id="ajaxStatusPanel"> <f:facet name="start"> <h:graphicImage value="./ajaxloading.gif" /> </f:facet> <f:facet name="complete"> <h:outputText value="" /> </f:facet> </p:ajaxStatus><h:form> <p:messages id="messages" showDetail="true" autoUpdate="true" closable="true" /> <p:spacer height="10" /> <p:panel> <h:panelGrid columns="2"> <p:commandButton value="Enter" action="#{messagingMB.sendMessage}" /> </h:panelGrid> </p:panel> <p:spacer height="10" /> </h:form> </h:body> </html> Pretty simple huh? The Managed Bean is also easy. import java.io.Serializable;import javax.annotation.PostConstruct; import javax.ejb.EJB; import javax.faces.bean.ManagedBean; import javax.faces.bean.ViewScoped;import org.jivesoftware.smack.XMPPException;@ManagedBean @ViewScoped public class MessagingMB implements Serializable {private static final long serialVersionUID = -9092497421080796430L; @EJB private JSFUtilEJB jsfUtilEJB; @PostConstruct public void init() { }public void sendMessage() { try { new BasicClient().test(); } catch (XMPPException e) { jsfUtilEJB.addErrorMessage(e,"Could not send"); } } }and of course, the EJBs: import javax.ejb.Stateless; import javax.faces.application.FacesMessage; import javax.faces.application.FacesMessage.Severity; import javax.faces.context.FacesContext;@Stateless public class JSFUtilEJB {@SuppressWarnings("unchecked") public <T> T findBean(String beanName) { FacesContext context = FacesContext.getCurrentInstance(); return (T) context.getApplication().evaluateExpressionGet(context, "#{" + beanName + "}", Object.class); }public long addErrorMessage(String msg) { return addMessage(null,FacesMessage.SEVERITY_ERROR,msg); } public long addErrorMessage(Exception e,String summary){ return addMessage(e,FacesMessage.SEVERITY_ERROR,summary); } public long addFatalErrorMessage(Exception e,String summary){ return addMessage(e,FacesMessage.SEVERITY_FATAL,summary); } public long addInfoMessage(String summary){ return addMessage(null,FacesMessage.SEVERITY_INFO,summary); } public long addWarnMessage(Exception e,String summary){ return addMessage(e,FacesMessage.SEVERITY_WARN,summary); }public long addErrorMessage(Exception e) { return addMessage(e,FacesMessage.SEVERITY_ERROR,e.getMessage(),e.getClass().getSimpleName()); }private long addMessage(Exception e,Severity severity, String summary) { FacesContext context = FacesContext.getCurrentInstance(); String clientId = null; long id = -1; if (e != null){ id = printStackTrace(e); FacesMessage facesMessage = null; if (e.getCause() instanceof org.apache.openjpa.persistence.EntityExistsException){ facesMessage = new FacesMessage(severity,"[Error: #"+id+"] "+summary,"You are trying are to add a new object that already exists or your're trying to violate a unique constraint)" ); }else{ facesMessage = new FacesMessage(severity,"[Error: #"+id+"] "+summary,e.getMessage() ); } context.addMessage(clientId , facesMessage ); }else{ FacesMessage facesMessage = new FacesMessage(severity,summary," "); context.addMessage(clientId , facesMessage ); }return id; }private long addMessage(Exception e,Severity severity, String summary, String detail) { FacesContext context = FacesContext.getCurrentInstance(); String clientId = null; long id = -1; if (e != null){ id = printStackTrace(e); FacesMessage facesMessage = new FacesMessage(severity,"["+id+"] "+summary,detail ); context.addMessage(clientId , facesMessage ); }else{ FacesMessage facesMessage = new FacesMessage(severity,summary,detail ); context.addMessage(clientId , facesMessage ); } return id; }public long printStackTrace(Exception e){ long uniqueId = System.currentTimeMillis(); return uniqueId; }}andimport java.io.File; import java.io.Serializable;import javax.annotation.PostConstruct; import javax.ejb.Singleton; import javax.ejb.Startup;import org.apache.vysper.mina.TCPEndpoint; import org.apache.vysper.storage.StorageProviderRegistry; import org.apache.vysper.storage.inmemory.MemoryStorageProviderRegistry; import org.apache.vysper.xmpp.addressing.EntityImpl; import org.apache.vysper.xmpp.authorization.AccountManagement; import org.apache.vysper.xmpp.server.XMPPServer;@Startup @Singleton public class XmppEJB implements Serializable { /** * <br>06/09/2014 */ private static final long serialVersionUID = 1L; private boolean started;@PostConstruct public void init() { try { // choose the storage you want to use // StorageProviderRegistry providerRegistry = new JcrStorageProviderRegistry(); StorageProviderRegistry providerRegistry = new MemoryStorageProviderRegistry(); final AccountManagement accountManagement = (AccountManagement) providerRegistry.retrieve(AccountManagement.class); if(!accountManagement.verifyAccountExists(EntityImpl.parse("user1@vysper.org"))) { accountManagement.addUser(EntityImpl.parse("user1@vysper.org"), "password"); } if(!accountManagement.verifyAccountExists(EntityImpl.parse("user2@vysper.org"))) { accountManagement.addUser(EntityImpl.parse("user2@vysper.org"), "password"); } XMPPServer server = new XMPPServer("vysper.org"); server.addEndpoint(new TCPEndpoint()); server.setStorageProviderRegistry(providerRegistry); server.setTLSCertificateInfo(new File("/path/to/bogus_mina_tls.cert"), "boguspw"); server.start(); System.out.println("server is running..."); } catch (Exception e) { e.printStackTrace(); } started = true; }public boolean isStarted() { return this.started; } }and the basic client, which comes from Vysper. import java.util.Date;import org.jivesoftware.smack.Chat; import org.jivesoftware.smack.ConnectionConfiguration; import org.jivesoftware.smack.MessageListener; import org.jivesoftware.smack.PacketListener; import org.jivesoftware.smack.Roster; import org.jivesoftware.smack.SASLAuthentication; import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.filter.PacketFilter; import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.Packet; import org.jivesoftware.smack.packet.PacketExtension; import org.jivesoftware.smack.packet.Presence; import org.jivesoftware.smackx.packet.Time; import org.jivesoftware.smackx.packet.Version;public class BasicClient {static class IQListener implements PacketListener {public void processPacket(Packet packet) { IQ iq = (IQ) packet; String iqString = iq.toString(); System.out.println("T" + System.currentTimeMillis() + " IQ: " + iqString + ": " + iq.toXML()); } }static class PresenceListener implements PacketListener {public void processPacket(Packet packet) { Presence presence = (Presence) packet; String iqString = presence.toString(); final PacketExtension extension = presence .getExtension("http://jabber.org/protocol/caps"); if (extension != null) System.out.println("T" + System.currentTimeMillis() + " Pres: " + iqString + ": " + presence.toXML()); } }public void test() throws XMPPException {String me = "user2@vysper.org"; String to = "user1@vysper.org";try { ConnectionConfiguration connectionConfiguration = new ConnectionConfiguration( "localhost"); connectionConfiguration.setCompressionEnabled(false); connectionConfiguration.setSelfSignedCertificateEnabled(true); connectionConfiguration.setExpiredCertificatesCheckEnabled(false); // connectionConfiguration.setDebuggerEnabled(true); connectionConfiguration .setSecurityMode(ConnectionConfiguration.SecurityMode.required); // XMPPConnection.DEBUG_ENABLED = true; XMPPConnection connection = new XMPPConnection( connectionConfiguration); connection.connect();SASLAuthentication saslAuthentication = connection .getSASLAuthentication(); saslAuthentication.authenticate(me, "password", "test"); connection.login(me, "pqssword");connection.getRoster().setSubscriptionMode( Roster.SubscriptionMode.accept_all);connection.addPacketListener(new IQListener(), new PacketFilter() { public boolean accept(Packet packet) { return packet instanceof IQ; } });connection.addPacketListener(new PresenceListener(), new PacketFilter() { public boolean accept(Packet packet) { return packet instanceof Presence; } });Chat chat = null; if (to != null) { Presence presence = new Presence(Presence.Type.subscribe); presence.setFrom(connection.getUser()); String toEntity = to; presence.setTo(toEntity); connection.sendPacket(presence);chat = connection.getChatManager().createChat(toEntity, new MessageListener() { public void processMessage(Chat inchat, Message message) { System.out.println("log received message: " + message.getBody()); } }); }connection.sendPacket(new Presence(Presence.Type.available, "pommes", 1, Presence.Mode.available));Thread.sleep(1000);// query server version sendIQGetWithTimestamp(connection, new Version());// query server time sendIQGetWithTimestamp(connection, new Time());chat.sendMessage("Hello " + to + " at " + new Date());connection.disconnect(); } catch (Throwable e) { e.printStackTrace(); // To change body of catch statement use File | // Settings | File Templates. } System.out.println("bye"); }private static void sendIQGetWithTimestamp(XMPPConnection connection, IQ iq) { iq.setType(IQ.Type.GET); connection.sendPacket(iq); System.out.println("T" + System.currentTimeMillis() + " IQ request sent"); } } We’re almost set. Of course, now we need a XMPP client, such as Pidgin. First, let me just say thanks to this blog, because I don’t know why, Vysper site has little to no information about how to configure Pidgin, so this blog post was really useful. Let me show you how my pidgin user looks like:  I know, it’s in Portuguese. And that’s it. We’re all set. Start your JSF web application and play. Notice that the communication is bidirectional, so you can just use your XMPP client to send commands to the server. To do that, you just have to change this listener: chat = connection.getChatManager().createChat(toEntity, new MessageListener() { public void processMessage(Chat inchat, Message message) { System.out.println("log received message: " + message.getBody()); } });I wonder if we could just create a DSL to process some commands and if we could find some autocomplete pidgin plugin to write commands using this DSL. Suggestions are welcome! ps. The EJB does not shutdown the server gracefully. But I bet there’s some EJB annotation to do that in an event of a server shutdown.Reference: Embedding a XMPP server inside your JSF Web Application using Vysper, TomEE and PrimeFaces from our JCG partner Leonardo Shikida at the Leo’s Notepad blog....

jQuery Ajax – Servlets Integration: Building a complete application

There are many tutorials around the web that explain some stuff about web development in Java using servlets and JSP pages, however I have never found a concise, simple enough for beginers, tutorial. A tutorial like that should explain the whole process of creating a simple web app, including the frontend, the backend, and most importantly, the ways that someone can use to interact with both of them. It is not enough to show how to get information from the server, it is also important to kn0w how to differentiate between the information in a structured way, as well as to know how to make changes to the backend through the application environment. What we want to achieve by this post here, is to guide through the whole process of creating a complete “toy” web application. It is a “toy” application in the sense that it does only two things and we are using no extra features to make the environment beautiful. The purpose of the application will be simple:Add a band name with a list of albums (separated by commas) and press the “Submit” button to add them to the database. Press the “Show bands!” button to get a list of the bands, or the “Show bands and albums!” button to get a list of bands with their albums.The look of the app is as bare-bones as possible, but the code behind it is everything you need to start creating your own dynamic web applications, which are most commonly called CRUD applications (Create, Read, Update, Delete). They are called that way because all their functionality can be abstracted to these very basic commands. Before we start creating the application step-by-step, let’s take a look at all the tools that we are going to work with in this example:Eclipse Luna Java 7 Tomcat 7 (web application server) Gson 2.3 (Google Java library) JQuery 2.1.1 (Javascript library)1. Frontend (JSP page) There is not much to say here. If you have followed other examples, you will know how to easily create a Dynamic Web Project in Eclipse, and create an index.jsp page inside the folder WebContent. This will be the main page of our application, and we will not use any other page. Of course it always depends on the type of application that you need to create, but for our needs here one page is enough. index.jsp <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Ajax - Servlets Integration Example</title> <!-- Load the scripts needed for the application. --> <script type="text/javascript" src="resources/jquery-2.1.1.min.js"></script> <script type="text/javascript" src="resources/buttonEventsInit.js"></script> <script type="text/javascript" src="resources/resultsPrinter.js"></script> <script type="text/javascript" src="resources/insertBandInfo.js"></script> </head> <body> <h1>Ajax - Servlets Integration Example</h1> <p>This is an example of how to use Ajax with a servlet backend.</p></br> <h3>Select a button to get the relevant information.</h3> <!-- Buttons that will call the servlet to retrieve the information. --> <button id="bands" type="button">Show bands!</button> <button id="bands-albums" type="button">Show bands and albums!</button> <!-- We need to have some empty divs in order to add the retrieved information to them. --> <div id="band-results"></div></br></br> <div id="bands-albums-results"></div></br></br> <h3>Add the band information and press submit!</h3> <h4>Band name: </h4><input type="text" id="band-name-input" value=""><br> <h4>Albums: </h4><input type="text" id="album-input" value="">(Separated by commas)<br> <input type="submit" id="submit-band-info" value="Submit"> </body> </html>So, pretty simple stuff as you can see. We load the javascript files that we are going to need, out of which only one is external, JQuery. JQuery is a Javascript library that is as well known, as it is powerful. It allows us to easily get information about various elements of our page, as well as bind events to these elements. Take this code snippet here for a simple example: $("#my-button").click(function() { alert("My button was clicked!"); });This snippet here means: “When the element with an id of “my-button” is clicked, I want to trigger a function which creates an alert pop-up saying “My button was clicked!” “. So we pass a whole function that does something, as an argument to the event binding. We will explain more about JQuery later on. You can download JQuery here. There are some things to take into consideration here:We have given an id to all the important elements that we are going to use. So the buttons, the input boxes and the empty <div> elements all have a unique id. We created 2 empty <div> elements that will contain the results. This design pattern is often followed in cases you need a container for things and you need that container to be always there in a specific position. That way, we don’t need to check where we will put the information, as there is a position in the page reserved for that. Also, the second div (bands with albums) will always be under the first one (band names only). When we press the button for the bands only information, it will be added on top of the bands with albums.2. Asking the server for data (frontend and backend) 2.1 Making a GET request using Ajax, from the frontend. So, the first thing we need to do is find a way to ask the server for the data that we need, in this case Band names or Bands and Albums. We have already added two ids to the respective buttons (“bands” and “bands and albums”) so we need to bind an event to these in order to make a call to the server every time a button is pressed. We will use some Javascript for that, contained in the buttonEventsInit.js file. NOTE: Every Javascipt file is saved under the directory WebContent/resources, in order to give access to the browser to get them. buttonEventsInit.js // When the page is fully loaded... $(document).ready(function() { // Add an event that triggers when ANY button // on the page is clicked... $("button").click(function(event) { // Get the button id, as we will pass it to the servlet // using a GET request and it will be used to get different // results (bands OR bands and albums). var buttonID = event.target.id; // Basic JQuery Ajax GET request. We need to pass 3 arguments: // 1. The servlet url that we will make the request to. // 2. The GET data (in our case just the button ID). // 3. A function that will be triggered as soon as the request is successful. // Optionally, you can also chain a method that will handle the possibility // of a failed request. $.get('DBRetrievalServlet', {"button-id": buttonID}, function(resp) { // on sucess // We need 2 methods here due to the different ways of // handling a JSON object. if (buttonID === "bands") printBands(resp); else if (buttonID === "bands-albums") printBandsAndAlbums(resp); }) .fail(function() { // on failure alert("Request failed."); }); }); });Let’s explain what happens here. As soon as the page is loaded (we do this to be sure that all the elements are in place), we bind a click event to every button element in the page. From now on, every time a button is clicked, a GET request will be sent to the server with the information of which button was pressed. The server will send back the right response (in the form of a JSON object, we will explain later about that) and we will do different things to this object depending on the button that was pressed (because each button will receive a differently structured JSON object). Check out the comments on the example above regarding the right way to make a GET request to the server. You will need to provide the URL (which can also be the URL of a servlet), the data, and the function that will be triggered, having as an  argument the response (JSON object) of the server.2.2 Processing the request and sending the data back to the client. So, what happens to the server when we make a request? We are using a number of classes here, so let’s remember again that we are building an app that will contain two kinds of catalogues: Bands, and Bands with albums. So we are using:MusicDatabase.java: A class that uses the Singleton pattern in order to provide a persistent object that will contain the information that needs to be sent back to the client. DBRetrievalServlet.java: A servlet that will be used to process the GET request and using other classes, provide a response with the queried information. BandWithAlbums.java: A class that will be used to create new “data-holding objects”, in our cases containing a band name, and a list of albums.So, let’s examine each of these classes and explain how they are used. DBRetrievalServlet.java package servlets; import informationClasses.MusicDatabase;import java.io.IOException;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("/DBRetrievalServlet") public class DBRetrievalServlet extends HttpServlet { @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // We set a specific return type and encoding // in order to take advantage of the browser capabilities. response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); // Depending on the GET parameters, passed from the Ajax call, // we are able to differentiate the requests and call the appropriate // method. We can always use more classes for more use-cases. // The response object returns the information (as a JSON object in String form) // to the browser. String buttonID = request.getParameter("button-id"); switch (buttonID) { case "bands": response.getWriter().write(MusicDatabase .getInstance() .getBands()); break; case "bands-albums": response.getWriter().write(MusicDatabase .getInstance() .getBandsAndAlbums()); break; } } }We get the parameter “button-id”, which is contained inside the information sent from the client (in the request object), and depending on what kind of button we pressed , we need a different kind of information, by calling the MusicDatabase instance and calling a different method each time. NOTE: The Singleton Pattern About the MusicDatabase instance… We are using the Singleton pattern, which means that because we only want to have 1 instance of the class, we do not create a new instance by calling the new keyword ourselves. Instead, we are calling a method from the MusicDatabase class itself and we ask for an instance.If the instance has not been created yet, we create one and return it. If it has been created, we return the existing instance.There is no access to the constructor externally, and no way to accidentally create another instance. You can find more information about the Singleton pattern online. MusicDatabase.java package informationClasses;import java.util.ArrayList; import java.util.List;import jsonObjects.BandWithAlbums;import com.google.gson.Gson;public class MusicDatabase { private List bandNames; private List bandsAndAlbums; // Singleton methods private static MusicDatabase dbInstance = null; protected MusicDatabase() { bandNames = new ArrayList<>(); bandsAndAlbums = new ArrayList<>(); } public static MusicDatabase getInstance() { if(dbInstance == null) { dbInstance = new MusicDatabase(); } return dbInstance; } public void setBandAndAlbums(String bandName, ArrayList bandAlbums) { bandNames.add(bandName); bandsAndAlbums.add(new BandWithAlbums(bandName, bandAlbums)); } public String getBands() { return new Gson().toJson(bandNames); } public String getBandsAndAlbums() { return new Gson().toJson(bandsAndAlbums); } }The methods we are going to examine here are getBands() and getBandsAndAlbums(). We only need these 2 methods, and they are pretty simple, because we have some things that help us here:Gson is a Java library from Google, which enables us to create JSON objects easily from a Java object. That object can be anything, from a simple data structure, to an objects that contains information an other data structures etc. In our case, we have 2 such datastructures:A List<String> bandNames, which contains only the band names in the form of Strings. A List<BandWithAlbums>, which contains objects, which in turn contain the band name and a list of their albums.The BandWithAlbums class, which enables us to hold more information about a band. It is a data-holding class, which contains the band name as a String and a list of their bands as a List<String>. By returning this object, you return all the associated information as well.All in all, The Gson library, using the command new Gson().toJson(Object obj), can transform most of the Objects and data structures into a JSON format, which the browser can easily use through Javascript. NOTE: You need to add the Gson library to the classpath for this to work. BandWithAlbums.java package jsonObjects;import java.util.ArrayList;public class BandWithAlbums {String bandName; ArrayList bandAlbums; public BandWithAlbums(String bandName, ArrayList bandAlbums) { this.bandName = bandName; this.bandAlbums = bandAlbums; } }A simple data-holding class, as we already said before. It represents “a band”, in the sense that it contains the name as well as the albums of the band.2.3 Presenting the data to the browser. So, as soon as we have the data that we asked for, we can see in the file buttonEventsInit.js, that we have the choice of calling two different methods, depending on the id of the button that made the call. We are going to show what these two methods are doing, and how we are going to present the data back to the browser. resultsPrinter.js // Both those functions get a json object as an argument, // which itself also holds other objects.// 1. The first function is supposed to get an object // containing just a list of band names. // 2. The second function is supposed to get an object containing // bands with albums, which essentially means a list of objects // which hold (1) a band name and (2) a list of albums.function printBands(json) { // First empty the <div> completely and add a title. $("#band-results").empty() .append("<h3>Band Names</h3>"); // Then add every band name contained in the list. $.each(json, function(i, name) { $("#band-results").append(i + 1, ". " + name + " </br>"); }); };function printBandsAndAlbums(json) { // First empty the <div> completely and add a title. $("#bands-albums-results").empty() .append("<h3>Band Names and Albums</h3>"); // Get each band object... $.each(json, function(i, bandObject) { // Add to the <div> every band name... $("#bands-albums-results").append(i + 1, ". " + bandObject.bandName + " </br>"); // And then for every band add a list of their albums. $.each(bandObject.bandAlbums, function(i, album) { $("#bands-albums-results").append("--" + album + "</br>"); }); }); };In order to understand how these functions work, we have to take a look at the response objects that the server returned to the client. In the first case, we are expecting just a list of band names, so the expected object will be just a list: ["The Beatles", "Metallica"]On the other hand, in the second case we are expecting to receive the full band information, in which case the json object will look like this: [ { bandName: "The Beatles", bandAlbums: ["White Album", "Let it be"] }, { bandName: "Metallica", bandAlbums: ["St Anger", "The Black Album"] } ]So we need two different ways of handling the request. In each case however, we empty() the div that we are going to use, and add the information that we just got from the server using some very handy JQuery functions. Thus, the first part of our application is complete. Take a look at the screenshots to see the results.3. Updating the server from user input (frontend and backend) 3.1 Making a POST request using Ajax. In this part we are going to take a look on how to send data to the server. We already got a hint of how things work by processing the GET request on the previous part of the tutorial, and the steps here are not really that different, with the sole exception of allowing the user to provide the input for the application. Let’s take a look at the code we are using, and the functionality of each file. insertBandInfo.js $(document).ready(function() { // Add an event that triggers when the submit // button is pressed. $("#submit-band-info").click(function() { // Get the text from the two inputs. var bandName = $("#band-name-input").val(); var albumName = $("#album-input").val(); // Fail if one of the two inputs is empty, as we need // both a band name and albums to make an insertion. if (bandName === "" || albumName === "") { alert("Not enough information for an insertion!"); return; } // Ajax POST request, similar to the GET request. $.post('DBInsertionServlet',{"bandName": bandName, "albumName": albumName}, function() { // on success alert("Insertion successful!"); }) .fail(function() { //on failure alert("Insertion failed."); }); }); });If you followed the previous part of the tutorial, it is really easy to understand what we are doing here. Another click event, now targeted only at the specific id of the Submit button, which, after checking that the two input boxes actually have input in them, makes a POST request (to a new servlet, specifically used for that reason) sending the data that we want (band name and list of albums). 3.2 Saving the user input in our “database”. DBInsertionServlet.java package servlets;import informationClasses.MusicDatabase;import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Map;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("/DBInsertionServlet") public class DBInsertionServlet extends HttpServlet {@Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Map<String, String[]> bandInfo = request.getParameterMap(); // In this case here we are not using the data sent to just do different things. // Instead we are using them as information to make changes to the server, // in this case, adding more bands and albums. String bandName = Arrays.asList(bandInfo.get("bandName")).get(0); String albums = Arrays.asList(bandInfo.get("albumName")).get(0);MusicDatabase.getInstance() .setBandAndAlbums(bandName, getAlbumNamesFromString(albums)); // return success response.setStatus(200); }// Split the album String in order to get a list of albums. private ArrayList getAlbumNamesFromString(String albums) { return new ArrayList(Arrays.asList(albums.split(","))); } }When the servlet gets the request, it extracts the bandName from the request map, and a String containing the album names. We create a list of Albums by splitting the String into parts, when we find a comma. In the end we call the MusicDatabase instance where we add the band name and the album list, and if you check out the class definition from before, you can see that:We add the band name to the bandNames list. We create a new Band object (using the name and the list of albums) and we add it to the bandsWithalbums list.After that the servlet is done and it sends a SUCCESS status response back to the client. We have added everything to our lists and it is ready to be sent in a JSON format when we ask for it. Case in point, let’s see what happens if I add a new band myself.4. Download the project This was an example of Ajax – Servlets integration. I would like to think that I helped you gain a complete picture of how to implement every part of a web application (frontend-backend) and the simplest ways to attach the parts together in order to create software that allows user input and changes on the server, as well as on the client! You can download the full source code of this example here : AjaxServletsIntegration ...

Akka Notes – Actor Messaging – 1

From the introductory first part of the Akka Notes, we saw a bird’s eye view of Actors in the Akka Toolkit. In this second part of the Akka Notes, we’ll look at the messaging part of Actors. As for the example, we would use the same Student-Teacher example that we discussed earlier. In this first part of Actor Messaging, we’ll create the Teacher Actor and instead of the Student Actor, we’ll use a main program called StudentSimulatorApp.         Revisiting Student-Teacher in Detail Let’s for now consider the message sent by the StudentSimulatorApp to the TeacherActor alone. When I say StudentSimulatorApp, I just mean a normal main program.The picture conveys this : (if the terms are overwhelming, don’t worry, we’ll go through them in detail)Student creates something called an ActorSystem It uses the ActorSystem to create something called as ActorRef. The QuoteRequest message is sent to the ActorRef (a proxy to TeacherActor) Actor ref passes the message along to a Dispatcher The Dispatcher enqueues the message in the target Actor’s MailBox. The Dispatcher then puts the Mailbox on a Thread (more on that in the next section). The MailBox dequeues a message and eventually delegates that to the actual Teacher Actor’s receive method.Like I said, don’t worry about it. Let’s look at each step in detail now. You can come back and revisit these five steps once we are done. The StudentSimulatorApp program We would use this StudentSimulatorApp to bring up the JVM and initialize the ActorSystem.As we understand from the picture, the StudentSimulatorAppCreates an ActorSystem Uses the ActorSystem to create a proxy to the Teacher Actor (ActorRef) Sends the QuoteRequest message to the proxy.Let’s focus on these three points alone now.Creating an ActorSystemActorSystem is the entry point into the ActorWorld. ActorSystems are through which you could create and stop Actors. Or even shutdown the entire Actor environment. On the other end of the spectrum, Actors are hierarchical and the ActorSystem is also similar to the java.lang.Object or scala.Any for all Actors – meaning, it is the root for all Actors. When you create an Actor using the ActorSystem’s actorOf method, you create an Actor just below the ActorSystem.The code for initializing the ActorSystem looks like: val system=ActorSystem("UniversityMessageSystem") The UniversityMessageSystem is simply a cute name you give to your ActorSystem.Creating a Proxy for TeacherActor?Let’s consider the following snippet : val teacherActorRef:ActorRef=actorSystem.actorOf(Props[TeacherActor]) The actorOf is the Actor creation method in ActorSystem. But, as you can see, it doesn’t return a TeacherActor which we need. It returns something of type ActorRef. The ActorRef acts as a Proxy for the actual Actors. The clients do not talk directly with the Actor. This is Actor Model’s way of avoiding direct access to any custom/private methods or variables in the TeacherActor or any Actor for that sake. To repeat, you send messages only to the ActorRef and it eventually reaches your actual Actor. You can NEVER talk to your Actor directly. People will hate you to death if you find some mean ways to do that.Send a QuoteRequest to the ProxyIt’s an one liner again. You just tell the QuoteRequest message to the ActorRef. The tell method in Actor is actually !. (there’s also a tell method in ActorRef which just delegates the call back to !) //send a message to the Teacher Actor teacherActorRef!QuoteRequest That’s it !!!If you think I am lying, check the entire code of the StudentSimulatorApp below : StudentSimulatorApp.scala package me.rerun.akkanotes.messaging.firenforgetimport akka.actor.ActorSystem import akka.actor.Props import akka.actor.actorRef2Scala import me.rerun.akkanotes.messaging.protocols.TeacherProtocol._object StudentSimulatorApp extends App{//Initialize the ActorSystem val actorSystem=ActorSystem("UniversityMessageSystem")//construct the Teacher Actor Ref val teacherActorRef=actorSystem.actorOf(Props[TeacherActor])//send a message to the Teacher Actor teacherActorRef!QuoteRequest//Let's wait for a couple of seconds before we shut down the system Thread.sleep (2000)//Shut down the ActorSystem. actorSystem.shutdown()} Well, I cheated a little. You’ll have to shutdown the ActorSystem or otherwise, the JVM keeps running forever. And I am making the main thread sleep for a little while just to give the TeacherActor to finish off it’s task. I know this sounds stupid. Don’t worry about it. We’ll write some neat testcases in the next part in order to avoid this hack. The Message We just told a QuoteRequest to the ActorRef but we didn’t see the message class at all !! Here it comes : (It is a recommended practice to wrap your messages in a nice object for easier organization) TeacherProtocol package me.rerun.akkanotes.messaging.protocolsobject TeacherProtocol{case class QuoteRequest() case class QuoteResponse(quoteString:String)} As you know, the QuoteRequest is for the requests that come to the TeacherActor. The Actor would respond back with a QuoteResponse. Dispatcher and a MailBox The ActorRef delegates the message handling functionality to the Dispatcher. Under the hood, while we created the ActorSystem and the ActorRef, a Dispatcher and a MailBox was created. Let’s see what they are about.MailBox Ever Actor has one MailBox (we’ll see one special case later). Per our analogy, every Teacher has one mailbox too. The Teacher has to check the mailbox and process the message. In Actor world, it’s the other way round – the mailbox, when it gets a chance uses the Actor to accomplish its work. Also the mailbox has a queue to store and process the messages in a FIFO fashion – a little different from our regular inbox where the most latest is the one at the top. Now, the dispatcher Dispatcher does some really cool stuff. From the looks of it, the Dispatcher just gets the message from the ActorRef and passes it on to the MailBox. But there’s one amazing thing happening behind the scenes : The Dispatcher wraps an ExecutorService (ForkJoinPool or ThreadPoolExecutor). It executes the MailBox against this ExecutorService. Check out this snippet from the Dispatcher: protected[akka] override def registerForExecution(mbox: Mailbox, ...): Boolean = { ... try { executorService execute mbox ... } What? Did you just say you execute the MailBox? Yup. We already saw that the MailBox holds all the messages in a Queue. Also since the Executor runs the MailBox, the MailBox must be a Thread. You’re right. That’s pretty much MailBox’s declaration and constructor. Here’s the signature of the Mailbox: private[akka] abstract class Mailbox(val messageQueue: MessageQueue) extends SystemMessageQueue with Runnable Teacher ActorThe MailBox, when it gets its run method fired, dequeues a message from the message queue and passes it to the Actor for processing. The method that eventually gets called when you tell a message to an ActorRef is the receive method of the target Actor. The TeacherActor is a rudimentary class which has a List of quotes and obviously the receive method which handles the messages. Check this out : TeacherActor.scala package me.rerun.akkanotes.messaging.firenforgetimport scala.util.Randomimport akka.actor.Actor import me.rerun.akkanotes.messaging.protocols.TeacherProtocol._/* * Your Teacher Actor class. * * The class could use refinement by way of * using ActorLogging which uses the EventBus of the Actor framework * instead of the plain old System out * */class TeacherActor extends Actor {val quotes = List( "Moderation is for cowards", "Anything worth doing is worth overdoing", "The trouble is you think you have time", "You never gonna know if you never even try")def receive = {case QuoteRequest => {import util.Random//Get a random Quote from the list and construct a response val quoteResponse=QuoteResponse(quotes(Random.nextInt(quotes.size)))println (quoteResponse)}}} The TeacherActor’s receive method pattern matches for just one Message – the QuoteRequest (actually, it is a good practice to pattern match the default case but there’s an interesting story to tell there) All that the receive method does is topattern match for QuoteRequest pick a random quote from the static list of quotes construct a QuoteResponse print the QuoteResponse to the consoleCodeThe entire project could be downloaded from github here.We’ll cover more interesting stuff in the parts coming up…Reference: Akka Notes – Actor Messaging – 1 from our JCG partner Arun Manivannan at the Rerun.me blog....

Hazelcast’s MapLoader pitfalls

One of the core data structures provided by Hazelcast is IMap<K, V> extendingjava.util.concurrent.ConcurrentMap – which is basically a distributed map, often used as cache. You can configure such map to use custom MapLoader<K, V> – piece of Java code that will be asked every time you try to .get()something from that map (by key) which is not yet there. This is especially useful when you use IMap as a distributed in-memory cache – if client code asks for something that wasn’t cached yet, Hazelcast will transparently execute yourMapLoader.load(key):         public interface MapLoader<K, V> { V load(K key); Map<K, V> loadAll(Collection<K> keys); Set<K> loadAllKeys(); }The remaining two methods are used during startup to optionally warm-up cache by loading pre-defined set of keys. Your custom MapLoader can reach out to (No)SQL database, web-service, file-system, you name it. Working with such a cache is much more convenient because you don’t have to implement tedious “if not in cache load and put in cache” cycle. Moreover, MapLoader has a fantastic feature – if many clients are asking at the same time for the same key (from different threads, or even different cluster members – thus machines), MapLoader is executed only once. This significantly decreases load on external dependencies, without introducing any complexity. In essence IMap with MapLoader is similar to LoadingCache found in Guava – but distributed. However with great power comes great frustration, especially when you don’t understand the peculiarities of API and inherent complexity of a distributed system. First let’s see how to configure custom MapLoader. You can use hazelcast.xml for that (<map-store/> element), but you then have no control over life-cycle of your loader (e.g. you can’t use Spring bean). A better idea is to configure Hazelcast directly from code and pass an instance of MapLoader: class HazelcastTest extends Specification { public static final int ANY_KEY = 42 public static final String ANY_VALUE = "Forty two"def 'should use custom loader'() { given: MapLoader loaderMock = Mock() loaderMock.load(ANY_KEY) >> ANY_VALUE def hz = build(loaderMock) IMap<Integer, String> emptyCache = hz.getMap("cache")when: def value = emptyCache.get(ANY_KEY)then: value == ANY_VALUEcleanup: hz?.shutdown() }Notice how we obtain an empty map, but when asked for ANY_KEY, we get ANY_VALUE in return. This is not a surprise, this is what our loaderMock was expected to do. I left Hazelcast configuration: def HazelcastInstance build(MapLoader<Integer, String> loader) { final Config config = new Config("Cluster") final MapConfig mapConfig = config.getMapConfig("default") final MapStoreConfig mapStoreConfig = new MapStoreConfig() mapStoreConfig.factoryImplementation = {name, props -> loader } as MapStoreFactory mapConfig.mapStoreConfig = mapStoreConfig return Hazelcast.getOrCreateHazelcastInstance(config) }Any IMap (identified by name) can have a different configuration. However special "default" map specifies default configuration for all maps. Let’s play a bit with custom loaders and see how they behave when MapLoader returns nullor throws an exception: def 'should return null when custom loader returns it'() { given: MapLoader loaderMock = Mock() def hz = build(loaderMock) IMap<Integer, String> cache = hz.getMap("cache")when: def value = cache.get(ANY_KEY)then: value == null !cache.containsKey(ANY_KEY)cleanup: hz?.shutdown() }public static final String SOME_ERR_MSG = "Don't panic!"def 'should propagate exceptions from loader'() { given: MapLoader loaderMock = Mock() loaderMock.load(ANY_KEY) >> {throw new UnsupportedOperationException(SOME_ERR_MSG)} def hz = build(loaderMock) IMap<Integer, String> cache = hz.getMap("cache")when: cache.get(ANY_KEY)then: UnsupportedOperationException e = thrown() e.message.contains(SOME_ERR_MSG)cleanup: hz?.shutdown() }MapLoader is executed in a separate thread So far nothing surprising. The first trap you might encounter is how threads interact here. MapLoader is never executed from client thread, always from a separate thread pool: def 'loader works in a different thread'() { given: MapLoader loader = Mock() loader.load(ANY_KEY) >> {key -> "$key: ${Thread.currentThread().name}"} def hz = build(loader) IMap<Integer, String> cache = hz.getMap("cache")when: def value = cache.get(ANY_KEY)then: value != "$ANY_KEY: ${Thread.currentThread().name}"cleanup: hz?.shutdown() }This test passes because current thread is "main" while loading occurs from within something like"hz.Cluster.partition-operation.thread-10". This is an important observation and is actually quite obvious if you remember that when many threads try to access the same absent key, loader is called only once. But more needs to be explained here. Almost every operation on IMap is encapsulated into one of operation objects (see also: Command pattern). This operation is later dispatched to one or all cluster members and executed remotely in a separate thread pool, or even on a different machine. Thus, don’t expect loading to occur in the same thread, or even same JVM/server (!) This leads to an interesting situation where you request given key on one machine, but actual loading happens on the other. Or even more epic – machines A, B and C request given key whereas machine D physically loads value for that key. The decision which machine is responsible for loading is made based on consistent hashing algorithm. One final remark – of course you can customize the size of thread pools running these operations, see Advanced Configuration Properties. IMap.remove() calls MapLoader This one is totally surprising and definitely to be expected once you think about it: def 'IMap.remove() on non-existing key still calls loader (!)'() { given: MapLoader loaderMock = Mock() def hz = build(loaderMock) IMap<Integer, String> emptyCache = hz.getMap("cache")when: emptyCache.remove(ANY_KEY)then: 1 * loaderMock.load(ANY_KEY)cleanup: hz?.shutdown() }Look carefully! All we do is removing absent key from a map. Nothing else. Yet, loaderMock.load() was executed. This is a problem especially when your custom loader is particularly slow or expensive. Why was it executed here? Look up the API of `java.util.Map#remove(): V remove(Object key) [...] Returns the value to which this map previously associated the key, or null if the map contained no mapping for the key. Maybe it’s controversial but one might argue that Hazelcast is doing the right thing. If you consider our map withMapLoader attached as sort of like a view to external storage, it makes sense. When removing absent key, Hazelcast actually asks our MapLoader: what could have been a previous value? It pretends as if the map contained every single value returned from MapLoader, but loaded lazily. This is not a bug since there is a special method IMap.delete() that works just like remove(), but doesn’t load “previous” value: @Issue("https://github.com/hazelcast/hazelcast/issues/3178") def "IMap.delete() doesn't call loader"() { given: MapLoader loaderMock = Mock() def hz = build(loaderMock) IMap<Integer, String> cache = hz.getMap("cache")when: cache.delete(ANY_KEY)then: 0 * loaderMock.load(ANY_KEY)cleanup: hz?.shutdown() }Actually, there was a bug: IMap.delete() should not call MapLoader.load(), fixed in 3.2.6 and 3.3. If you haven’t upgraded yet, even IMap.delete() will go to MapLoader. If you think IMap.remove() is surprising, check out howput() works! IMap.put() calls MapLoader If you thought remove() loading value first is suspicious, what about explicit put() loading a value for a given key first? After all, we are explicitly putting something into a map by key, why Hazelcast loads this value first via MapLoader? def 'IMap.put() on non-existing key still calls loader (!)'() { given: MapLoader loaderMock = Mock() def hz = build(loaderMock) IMap<Integer, String> emptyCache = hz.getMap("cache")when: emptyCache.put(ANY_KEY, ANY_VALUE)then: 1 * loaderMock.load(ANY_KEY)cleanup: hz?.shutdown() }Again, let’s restore to java.util.Map.put() JavaDoc: V put(K key, V value) [...] Returns: the previous value associated with key, or null if there was no mapping for key. Hazelcast pretends that IMap is just a lazy view over some external source, so when we put() something into an IMapthat wasn’t there before, it first loads the “previous” value so that it can return it. Again this is a big issue whenMapLoader is slow or expensive – if we can explicitly put something into the map, why load it first? Luckily there is a straightforward workaround, putTransient(): def "IMap.putTransient() doesn't call loader"() { given: MapLoader loaderMock = Mock() def hz = build(loaderMock) IMap<Integer, String> cache = hz.getMap("cache")when: cache.putTransient(ANY_KEY, ANY_VALUE, 1, TimeUnit.HOURS)then: 0 * loaderMock.load(ANY_KEY)cleanup: hz?.shutdown() }One caveat is that you have to provide TTL explicitly, rather then relying on configured IMap defaults. But this also means you can assign arbitrary TTL to every map entry, not only globally to whole map – useful. IMap.containsKey() involves MapLoader, can be slow or block Remember our analogy: IMap with backing MapLoader behaves like a view over external source of data. That’s why it shouldn’t be a surprise that containsKey() on an empty map will call MapLoader: def "IMap.containsKey() calls loader"() { given: MapLoader loaderMock = Mock() def hz = build(loaderMock) IMap<Integer, String> emptyMap = hz.getMap("cache")when: emptyMap.containsKey(ANY_KEY)then: 1 * loaderMock.load(ANY_KEY)cleanup: hz?.shutdown() }Every time we ask for a key that’s not present in a map, Hazelcast will ask MapLoader. Again, this is not an issue as long as your loader is fast, side-effect free and reliable. If this is not the case, this will kill you: def "IMap.get() after IMap.containsKey() calls loader twice"() { given: MapLoader loaderMock = Mock() def hz = build(loaderMock) IMap<Integer, String> cache = hz.getMap("cache")when: cache.containsKey(ANY_KEY) cache.get(ANY_KEY)then: 2 * loaderMock.load(ANY_KEY)cleanup: hz?.shutdown() }Despite containsKey() calling MapLoader, it doesn’t “cache” loaded value to use it later. That’s why containsKey()followed by get() calls MapLoader two times, quite wasteful. Luckily if you call containsKey() on existing key, it runs almost immediately, although most likely will require network hop. What is not so fortunate is the behaviour of keySet(),values(), entrySet() and few other methods before version 3.3 of Hazelcast. These would all block in case any key is being loaded at a time. So if you have a map with thousands of keys and you ask for keySet(), one slowMapLoader.load() invocation will block whole cluster. This was fortunately fixed in 3.3, so that IMap.keySet(),IMap.values(), etc. do not block, even when some keys are being computed at the moment.As you can see IMap + MapLoader combo is powerful, but also filled with traps. Some of them are dictated by the API, osme by distributed nature of Hazelcast, finally some are implementation specific. Be sure you understand them before implementing loading cache feature.Reference: Hazelcast’s MapLoader pitfalls from our JCG partner Tomasz Nurkiewicz at the Java and neighbourhood blog....
Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
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: