Jersey: pre- and post-exec hooks? - java

I'm currently using aspectj to handle transactions in my Jersey RESTful classes. But I want to dump it for simplicity and consistency reasons (less technologies -> more transparency, better IDE support). I also want the REST get method implementations to return a w3c document class, ie. having a post-exec hooks that transforms results.
Does Jersey provide pre- and post-exec hook that allow me to initiate and close transactions and perform transformations on results returned to the remote end?

Assuming you're using version 2.x, Jersey has built in support for this, using Event Listeners. You can use a RequestEventListener, and the RequestEvent give you access to all the goodies. Matter of fact, for transaction handling, this is actually the way Dropwizard handles it.
If the event handlers doesn't suit your needs, there is more of a "natural" AOP support from HK2 (which Jersey uses for DI). Check out this article for a complete example integrating with Jersey.

Related

Does Streams API functionality overlap with Spring Integration?

When introducing parallel processing to an application where multiple save entity calls are being made, I see prior dev has chosen to do it via Spring Integration using split().channel(MessageChannels.executor(Executors.newfixedThreadPool(10))).handle("saveClass","saveMethod").aggregate().get() - where this method is mapped to a requestChannel using #Gateway annotation. My question is this task seems to be simpler to do using the parallelStream() and forEach() method. Does IntergrationFlow provide any benefit in this case?
If you really do a plain in-memory data processing where Java's Stream API is enough, then indeed you don't need the whole messaging solution like Spring Integration. But if you deal with distributed requirements to process data from different systems, like from HTTP to Apache Kafka or DB, then it is better to use some tool which allows you smoothly to connect everything together. Also: no one stops you to use Stream API in the Spring Integration application. In the end all your code is Java anyway. Please, learn more what is EIP and why we would need a special framework to implement this messaging-based solutions: https://www.enterpriseintegrationpatterns.com/

Any java async tracing framework?

I'd like to trace my async application with some key checkpoint.
Is there any popular framework I can use?
For example, I may choose to use vert.x or any other java async framework. For each request/response cycle, I'd make some checkpoint to log something while these points might happen in different threads.
I'd like to see an aggregated view of 1 request to see what's going on. Supporting distributed case would be better but single JVM is good to go.
What you are looking for is OpenTracing. It's an API that allows you to have distributed tracing features in a way that is vendor agnostic.
For your specific case, you'd have to handle the context propagation yourself, as there's no other (reliable) way to do that on OpenTracing yet for teh async case. For other cases (sync JAX-RS, Servlets, Spring Boot, ...), it would be safe to use the native framework integration and/or the Java agent rules.
For Vert.x, you'll need to inject the span context into Vert.x' message, and extract this context later on.
There's an example of OpenTracing + Vert.x on the Hawkular APM example directory, that might help you get started. Note, however, that you might want to use another backend should you decide to move forward, as we (Hawkular APM team) decided to join forces with Jaeger for the OpenTracing backend.

Get Declarative Service at runtime

What is the correct way to get a service, hopefully using Declarative Service if possible, when you don't know the attributes of the service to request until after runtime?
The use case is analogous to having 3 bundles providing services version 1.0, 2.0 and 3.0 but not knowing which one will be consumed until the user chooses one in the UI. If the user chooses 2.0 the consumer will consume the stuff from bundle 2.0
We are using BND annotations, so something with them would be ideal, but i have a feeling that we need to use the OSGi API directly instead of using annotations or declarative services injection.
Finally, if it is relevant, this is more to get different versions of a resource (XML schema) rather than about different behaviour/implementations. The idea was that the service would be providing its internal resource, which would be different in each version, even though the code in the service itself would be the same
I've worked in a similar system before and we had our own "routing" system. Basically when you register the services add the version number in the meta-data. Then in this routing mechanism pick the correct service. Your services will need to implement a common interface and in the router inject a List of them.
The declarative model of the Declarative Services specification is a build-time model, not a run-time one. To do run-time dependency management, you either need to do it yourself with a ServiceTracker, or use a different dependency management solution.
As one of its authors, I have a preference for the Apache Felix Dependency manager [1] which allows you to "declare" dependencies in Java code (at run-time, for example based on a choice made by a user in the UI like you say).It does not use Bnd annotations, but the code still allows you to use a declarative style and provides features like injection and/or callbacks.
Another solution that allows this is Apache Felix iPOJO [2].
[1] http://felix.apache.org/documentation/subprojects/apache-felix-dependency-manager.html
[2] http://felix.apache.org/documentation/subprojects/apache-felix-ipojo.html

Creating java RESTful application

I want to create new RESTful application. I am bit confused about framework I can do it with spring+jersey , but can I do same application using jersey alone?
What is major difference between SpringREST and jersey?
Which is more convenient? why?
I've used both frameworks a bit. Spring is a large framework/API that covers many areas, one of which is rest services. Jersey on the other hand just covers rest. It's the reference implementation for the JAX-RS API (JSR 311 & JSR 339).
This is basically the "standard" way to do rest in Java. There are also other implementations like RestEasy. In theory your code will only need to reference the common JAX-RS interfaces meaning you ought to be able to swap to a different implementation later if required. This obviously only works if you don't become reliant on bespoke functionality that isn't part of the JAX-RS standard.
If you were to use Jersey, you might still decide you want spring. It can be useful just for its dependency injection alone. In this case you might have a JAX-RS class handling rest requests which then calls a spring service which has been injected. This is actually how I'm writing rest API's.
Whether you should use spring to write the rest services or JAX-RS is subjective and really up to you. Personally I went with the standard JAX-RS API because I found it was more focused on rest. The spring rest approach is basically an extension of spring-mvc which was originally intended for JSP's. I found things like error handling were easier using JAX-RS than spring-mvc. That said someone else may beg to differ. The other benefit is by following the standard in theory you have more flexibility in future if you want to switch to a different provider.
The main difference is that Jersey is standards-based, and Spring MVC is not, if that matters to you. Both are very good.
The main advantage I found in Jersey (I used 1.x) was that it could automatically use Jackson JSON Views automatically, and Spring MVC could not. Also, error handling in Spring MVC is kind of irritating, as error pages default to standard HTML.
There is another project you have not listed, and that is Spring Data + Spring HATEOAS, which is newer, but seems pretty good.

Simpler-than-JBoss way to have POJO RPC in Java with container session management

Currently, I only know a way of doing RPC for POJOs in Java, and is with the very complex EJB/JBoss solution.
Is there any better way of providing a similar functionality with a thiner layer (within or without a Java EE container), using RMI or something that can serialize and send full blown objects over the wire?
I'm not currently interested in HTTP/JSON serialization BTW.
EDIT: For clarification: I'm trying to replace an old EJB 2.1/JBoss 4 solution with something more easy to manage at the container level. I need to have entire control over the database(planning to use iBATIS which would allow me to use fairly complex SQL very easily), but the only things I want to keep over the wire are:
Invocation of lookup/data modification methods (automagic serialization goes here).
Transparent session control (authentication/authorization). I still have to see how to accomplish this.
Both items have to work as a whole, of course. No access should be granted to users without credentials.
Because I'm not very fond of writing webapps, I plan to build a GUI (Swing or SWT) that would only manage POJOs, do some reporting and invoke methods from the container. I want the serialization to be as easy as possible.
As is nearly always the case, Spring comes to the rescue. From the reference documentation, you will want to read Chapter 17. Remoting and web services using Spring.
There are several methods to choose from. The beauty of Spring is that all your interfaces and implementations are vanilla POJOs. The wiring into RMI or whatever is handled by Spring. You can:
Export services using RMI:
probably the simplest approach;
Use HTTP invoker: if remote access is an issue, this might be better for firewalls, etc than pure RMI; or
Use Web Services, in which case I would favour JAX-WS over JAX-RPC.
Spring has the additional benefit in that it can do the wiring for both the server and the client, easily and transparently.
Personally I would choose either (2) or (3). HTTP is network friendly. It's easy to deploy in a Web container. Jetty's long-lived connections give you the option over server push (effectively) over HTTP.
All of these methods allow complex objects to be sent across the wire but they are subtly different in this regard. You need to consider if your server and client are going to be distributed separately and whether it's an issue if you change the interface that you need to redistribute the class files. Or you can use a customized serialization solution (even XML) to avoid this. But that has issues as well.
Using a Web container will allow you to easily plug-in Spring Security, which can be a bit daunting at first just because there are so many options. Also, HttpSession can be used to provide state information between requests.
Simple RPC is exactly what RMI was built for. If you make a serializable interface, you can call methods on one app from another app.
If you only need value objects then just ensure the POJOs implement Serializable and write the objects across sockets (using ObjectOutputStream). On the receiving end read the objects using ObjectInputStream. The receiving end has to have a compatible version of the POJO (see serialVersionUID).
Hessian/Burlap 'protocol-ize this: http://hessian.caucho.com/ and http://www.caucho.com/resin-3.0/protocols/burlap.xtp
You could try XStream (http://x-stream.github.io/) over REST. Easy to apply on e pre-existing set of pojos.
Can you give some further information as to what you're trying to achieve, since you're not interested in rest/json ?

Categories

Resources