Adam Bien's Weblog

Project connectorZ--Lean JCA 1.6 File Store

connectorZ project is an example connector implementation under Apache 2.0 license.

After a work manager implementation, the second file store sample is available. It is a key-value store like, transactional, JCA connector which accesses files locally:

 1 package com.abien.filestore;
 3 import;
 4 import javax.annotation.Resource;
 5 import javax.ejb.Stateless;
 6 import*;
 7 import;
 8 import;
 9 import org.connectorz.files.Bucket;
10 import org.connectorz.files.BucketStore;
12 /**
13  *
14  * @author adam bien,
15  */
16 @Path("files")
17 @Stateless
18 @Consumes(MediaType.TEXT_PLAIN)
19 @Produces(MediaType.TEXT_PLAIN)
20 public class FilesResource {
22     @Resource(name = "jca/files")
23     BucketStore bucketStore;
25     @PUT
26     @Path("{id}")
27     public Response put(@PathParam("id") String id, String content) {
28         try (Bucket bucket = bucketStore.getBucket();) {
29             bucket.write(id, content.getBytes());
30         }
31         URI createdURI = URI.create(id);
32         return Response.created(createdURI).build();
33     }
35     @GET
36     @Path("{id}")
37     public String fetch(@PathParam("id") String id) {
38         try (Bucket bucket = bucketStore.getBucket();) {
39             final byte[] content = bucket.fetch(id);
40             if(content == null)
41                 return null;
42             return new String(content);
43         }
44     }
46     @DELETE
47     @Path("{id}")
48     public void delete(@PathParam("id") String id) {
49         try (Bucket bucket = bucketStore.getBucket();) {
50             bucket.delete(id);
51         }
52     }
53 }

File access is prohibited by the EJB spec (and many cloud providers as well) for a good reason: multi-threaded, remote access to the file system may cause inconsistencies and deadlocks. With JCA a consistent file access realization is relatively easy--the container informs the JCA implementation about the current transaction status, what makes the synchronization with the file system easier.

[The connectorZ project was initiated by factoring out examples from the book "Real World Java EE Patterns--Rethinking Best Practices" book (Second Iteration), page 303 in, chapter "Generic (File) JCA"]

The source code was formatted with NetBeans "HTML printing"

See you at Java EE Workhops at MUC Airport (October 22nd-24th)!

NEW workshop: HTML 5 and JavaScript Essentials, Three days in April 2017: from Java EE 7 Architectures over Microservices to Performance, Troubleshooting and Monitoring
On demand workshops: Java EE 7: Bootstrap, Effective, Testing and Microservices available for streaming.


A book about rethinking Java EE Patterns


In the first place, thanks for this elegant example.
Both on GlassFish and Paraya 4.1.1 is runs quite well.

But: Did you try this on WildFly 10.1?

The BucketStore resource injection fails but the resource adapter is successfully deployed. Seems that the class-loader fails to make the api.jar visible to the war file.

Will try to workaround using the jboss-deployment-structure.xml. Useful details I found here:

Especially note
"EE.8.3.2 EJB Container Class Loading Requirements
Components in the EJB container must have access to the following classes and resources.
• ...
• The contents of all jar files included in each resource adapter archive(rar file) deployed separately to the application server, if that resource adapter is used to satisfy any resource references in the module.

What do you think? Could this be a bug in the WildFly implementation?

Posted by Jörg Thönnes on September 23, 2016 at 06:09 PM CEST #

OK, got the example running by adding an explicit module dependency:


<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
<module name="deployment.jca-file-store.rar" />

Does this indicate a spec violation in the WildFly implementation?

Posted by Jörg Thönnes on September 23, 2016 at 07:04 PM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
Online Workshops
...the last 150 posts
...the last 10 comments