Injecting Remote Properties With Java EE : Adam Bien's Weblog

Injecting Remote Properties With Java EE : Adam Bien's Weblog Adam Bien's Weblog « Java EE 7 Applicatio... | Main | Continous Deployment... » Injecting Remote Properties With Java EE In Java EE the meta data of an injection target is available as a an optional parameter. The instance javax.enterprise.inject.spi.InjectionPoint holds the class-, field information as well as used annotations: import javax.enterprise.inject.Produces; import javax.enterprise.inject.spi.InjectionPoint; @Produces public String getString(InjectionPoint ip) {} The following field's metadata provides sufficient information to perform a HTTP-GET request and inject the fetched value at runtime: @Inject @CacheEntry(host = "headlands", port = 8080, cache = "configuration", key = "message") String notexisting; The metadata extracted from above field is used to resolve the following HTTP template and access a remote HTTP-resource: http://{host}:{port}/headlands/resources/caches/{cache}/entries/{key} The JAX-RS 2+ client resolves the template and performs the request: import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.Response; import javax.ws.rs.client.WebTarget; private Client client; @PostConstruct public void init() { this.client = ClientBuilder.newClient(); } WebTarget resolve(CacheEntry cacheEntry) { return this.resolve(cacheEntry.host(), cacheEntry.port(), cacheEntry.cache(), cacheEntry.key()); } WebTarget resolve(String host, int port, String cache, String key) { return resolve(host, port, cache).path(key); } WebTarget resolve(String host, int port, String cache) { StringBuilder builder = new StringBuilder(); String hostUri = builder.append("http://"). append(host). append(":"). append(port).toString(); return this.client.target(hostUri + "/headlands/resources/caches/{cache}/entries/"). resolveTemplate("cache", cache); } Now you only have to "produce" the value fetched from a remote HTTP-location: @Produces @CacheEntry(cache = "-") public String getString(InjectionPoint ip) { Annotated annotated = ip.getAnnotated(); CacheEntry cacheEntry = annotated.getAnnotation(CacheEntry.class); return resolve(cacheEntry).request().get(String.class); } The URI used in this example is compatible with headlands -- a JCache exposed via REST. Project marina comprises a single class (you will find 80% above) and the CacheEntry annotation. Marina enables injection of entries stored in a remote headlands instance. Marina is available in maven central as following dependency (5.7 kB): <dependency> <groupId>com.airhacks</groupId> <artifactId>marina</artifactId> <version>0.0.1</version> </dependency> See you at Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting . Is Munich's airport too far? Learn from home: airhacks.io . Marina and headlands are used as an example in the Microservices with Java EE 7 and Java 8 on-demand workshop. Posted at 07:38PM Mar 15, 2016 by Adam Bien , Comments[0]  | Views/Hits: 73 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. Newsletter: airhacks.news A book about rethinking Java EE Patterns Comments: Post a Comment: Name: E-Mail: URL: Notify me by email of new comments Remember Information? Your Comment: HTML Syntax: NOT allowed Number of posts: 1615 Number of comments: 5897 Yesterday's hits: 28599 Today's hits: 7966 Post reads / hour: 1469 Top posts: Adam Bien 158371 Java 8: Reading A File Into A String : Adam Bien's Weblog 68200 Java 7+: Writing A String To File--A One Liner : Adam Bien's Weblog 44201 Adam Bien's Weblog 41347 Simplest Possible EJB 3.1 / REST (JSR-311) Component : Adam Bien's Weblog 22177 Client-Side HTTP Basic Access Authentication With JAX-RS 2.0 : Adam Bien's [...] 19746 Don't use JPA's RESOURCE_LOCAL on the server : Adam Bien's Weblog 15411 Trending (last hour): Installing Oracle JDBC-Driver On Wildfly / JBoss : Adam Bien's Weblog 4 Java 8: Reading A File Into A String : Adam Bien's Weblog 4 20+ Topics in 45 mins -- 35th airhacks.tv Questions and Answers : Adam Bien[...] 3 Java 8: Streaming A String : Adam Bien's Weblog 2 Mapping JPA Entities To SQL Views - It Works Even With Derby : Adam Bien's [...] 2 about.adam-bien.com blog archives realworldpatterns.com Real World Java EE Patterns Rethinking Best Practices Order from amazon.de Order from amazon.com Online Workshops ...the last 150 posts 20+ Topics in 45 mins -- 35th airhacks.tv Questions and Answers JPA and Microservices, RxJava, CRUD and TX, Gradle, Cross Field Validation, Licensing, MDA or Topics For The 35th airhacks.tv JSON Is The New Data Transfer Object (DTO) Asynchronous WAR To WAR Communication With WebSockets Creating A CustomElement (WebComponent) From Scratch 60k eBills per Hour, Tiny WARs -- Or