Adam Bien's Weblog

Wednesday Oct 17, 2012

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;
 2 
 3 import java.net.URI;
 4 import javax.annotation.Resource;
 5 import javax.ejb.Stateless;
 6 import javax.ws.rs.*;
 7 import javax.ws.rs.core.MediaType;
 8 import javax.ws.rs.core.Response;
 9 import org.connectorz.files.Bucket;
10 import org.connectorz.files.BucketStore;
11 
12 /**
13  *
14  * @author adam bien, adam-bien.com
15  */
16 @Path("files")
17 @Stateless
18 @Consumes(MediaType.TEXT_PLAIN)
19 @Produces(MediaType.TEXT_PLAIN)
20 public class FilesResource {
21 
22     @Resource(name = "jca/files")
23     BucketStore bucketStore;
24 
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     }
34 
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     }
45 
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 }
54 

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: Microservices with Java EE 7 and Java 8, January 26th, 2015, Airport Munich

A book about rethinking Java EE Patterns

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
realworldpatterns.com
...the last 150 posts
...the last 10 comments
License