I am going to develop a webservice which will expose two operations. These operation will query/update data from database.
Please suggest do i use EJB for database operation and what advantage i will get?
or
in my webservice i use JPA directly like following, and create my entities and persist them
#PersistenceUnit private EntityManagerFactory emf;
#Resource
private UserTransaction utx;
Please answer with advantages/disadvantages.
Regards,
imran
Both approaches are valid and supported by Java EE, so this is only a design recommendation based on my experience:
Do never directly expose EJB operations as Web Services, it only increases runtime complexity. If you publish an EJB as a Web Service the container must still wrap it by using an internal Web Services servlet which (implicitly) tightly couples your WAR containing the internal Web Service mapping to your ejb-jar (depends on app server product). Furthermore, it's hard to debug in day-to-day operations.
My recommendation using only standard Java EE features without any additional libraries:
POJO defines the Web Service interface, aka its operations (#WebService annotation). It delegates to a functional implementation. Let's call this POJO WS-POJO.
Two options to implement the functionality:
WS-POJO calls stateless Session Beans which provide the required functionality. Pro: All EJB features available (dependency injection, declarative transaction mgmt, etc.). Con: "Heavyweight"
WS-POJO calls custom POJOs following e.g. the Command Pattern. Pro: "Lightweight". Con: Dependency injection not possible; Entity Manager etc. to be passed.
Today, I'd go with option #1... my 2 cent
Related
i'm working on a project, where i have to decide to use EJB, Webservices or EJB with #WebService in a combined way. I'm new to Java EE and i like the concept of EJBs, but i want also provide a way for non-java-systems to use this service. Here are the facts about the major components:
There are components, which have to interact with the database (simple DAOs) and a little bit more aggregated inserts. They should be called only from other java components
I have component(s), which are using shared library on the application server with complex calculations. This library depends on two native Libraries. So i have to use JNI. In the literature, they said, that it is not recommend to work with EJBs and native libraries together (i don't know why). But in this case i have no other chance. These component(s) needs to interact with the DAOs and have to be available for non-java applications.
The resulting architecture should be expandable (with timer services in EJB or some other functionalities)
The plan was to implement all these components in EJBs, but the point with the native library is confusing me. So my question is now:
How should i implement the components with all the database stuff? (I would use EJB, because of the Pooling and the transaction stuff)
How should i implement the component(s), which are using the native library through JNI, but also have to interact with the database? (Jax-WS WebService, EJB declared as JAX-WS service?
Another option would be to put everything in one single WebService. But i would not like this approach, because of the transactional an pooling advantages in EJB and the restricted expandability. So i would be greatful, if you can give me your opinion, how you would implement this project.
This is my suggestion but I'm also beginner for JavaEE so there might be another way to do.
You need to first established
Parent POM by using Maven inside Parent POM project, put all your dependency then create a modules like
parent-project[pom]:
project-name-data.jar
project-name-services.jar
project-name-webservice.war
project-name-ui.war
by this way, You can use all your shared library on your application.
How should i implement the components with all the database stuff? (I would use EJB, because of the Pooling and the transaction stuff)
It will be done in project-name-data.jar and implement on project-name-services.jar
How should i implement the component(s), which are using the native library through JNI, but also have to interact with the database? (Jax-WS WebService, EJB declared as JAX-WS service?
It will be implement in project-name-webservice.war by adding JAX-WS service, project-name-services.jar dependency.
In Your project-name-ui.war add dependency of project-name-webservice.war to run your application fine.
If you not clear about this, then click here. it's my personal tryout which use EJB, Jax-RS and other libraries. Hope it will help you.
I'm creating a web-application and decided to use micro-services approach. Would you please tell me what is the best approach or at least common to organize access to the database from all web-services (login, comments and etc. web-services). Is it well to create DAO web-service and use only it to to read/write values in the database of the application. Or each web-service should have its own dao layer.
Each microservice should be a full-fledged application with all necessary layers (which doesn't mean there cannot be shared code between microservices, but they have to run in separate processes).
Besides, it is often recommended that each microservice have its own database. See http://microservices.io/patterns/data/database-per-service.html https://www.nginx.com/blog/microservices-at-netflix-architectural-best-practices/ Therefore, I don't really see the point of a web service that would only act as a data access facade.
Microservices are great, but it is not good to start with too many microservices right away. If you have doubt about how to define the boundaries between microservices in your application, start by a monolith (all the time keeping the code clean and a good object-oriented with well designed layers and interfaces). When you get to a more mature state of the application, you will more easily see the right places to split to independently deployable services.
The key is to keep together things that should really be coupled. When we try to decouple everything from everything, we end up creating too many layers of interfaces, and this slows us down.
I think it's not a good approach.
DB operation is critical in any process, so it must be in the DAO layer inside de microservice. Why you don't what to implement inside.
Using a service, you loose control, and if you have to change the process logic you have to change DAO service (Affecting to all the services).
In my opinion it is not good idea.
I think that using Services to expose data from a database is ideal due to the flexibility it provides. Development of a REST service to expose some or all of your data as a service provides flexibility to consume the data directly to the UI via AJAX or by other services which can process the data and generate new information. These consumers do not need to implement a DAO and can be in any language. While a REST Service of your entire database is probably not a Micro-Service, a case could be made for breaking this down as Read only for Students, Professors and Classes for exposing on the School Web site(s), with different services for Create, Update and Delete (CUD) available only to the Registrars office desktop applications.
For example building a Service to exposes a statistical value on data will protect the data from examination by a user/program who only needs a statistical value without the requirement of having the service implement an entire DAO for the components of that statistic. Full function databases like SQL Server or Oracle provide a lot of functionality that application developers can use, including complex queries(using indexes), statistics the application of set operations on data.
Having a database service is a completely valid pattern. In fact, this is one of the key examples of where to start to export aspects of a monolith to a micro service in the Building Microservices book.
How to organize your code around such idea is a different issue. Yes, from the db client programmer's stand point, having the same DAO layer on each DB client makes a lot of sense.
The DAO pattern may be suitable to bind your DB to one programming language that you use. But then you need to ask yourself why you are exposing your database as a web service if all access to it will be mediated by the same DAO infrastructure. Or are you going to create one DAO pattern for each client programming language binding?
If all database clients are going to be written on the same programming language, then are you sure you really need to wrap your DB as a microservice? After all, the DB is usually already a remote service with a well-defined network protocol optimized to transfer data fast and reliably. Why adding HTTP on top of it? What are you expecting to gain from adding such complexity?
Another problem with using the DAO pattern is that the DAO structure does not necessarily follow the evolution of the web service. The web service may evolve in a way that does not make old clients incompatible. You may have different clients using different features of the micro service. In this case you are not sharing the same DAO layer structure on each client.
Make sure you are not using RPC-style programming over web services, which does not make much sense. You will be basically throwing away one of the key advantages of micro services, which is the decoupling between service and client.
I have a server exposing multiple web services.
The objects of the services are scattered in several packages.
From my understanding if you have inheritance and a service can return a number of possible sub classes you need to use the #XmlSeeAlso on the base class which adds unnecessary dependencies and complexity.
For both performance and dynamic configuration reasons I want to create myself the JAXB context which will be used by the CXF client / server services.
This way I can avoid the unnecessary dependencies and manage the context myself.
I prefer specifying it once on the bus level, but if that isn't possible I guess controlling it on the service level will be good enough.
Thanks in advance,
avner
Interoperability comes to mind (MS/Java).
Also, with EJB you need to distribute EJB interface, with WS you got WSLD (I know there's EJB extension for WSDL, but I'm not sure it's used).
Anything else?
EJB is mostly about a programming model for how you implement callable Business Logic. You code is running in a container which looks after management, clustering, transactions and security. Your component can be called by and number of different mechansims including local Java Calls, RMI/IIOP for remote invocation and also Web Services, so yes your EJB can indeed have a WSDL and be callable fro other non-Java envrionments.
If you start instead from the point of view of having a WSDL, which probably will specify SOAP/HTTP, then you are free to implement that in many different technologies, and of cource invoke it via that specified protocol, which very many different clients can use. The big question is how easily you can deal with those quality of implementation issues - your chosen implementation environment may give a lot of help or leave a lot to you.
Summary: you're not really comparing like-with-like. Web Services is very about the interface, EJB very much about the implementation.
What exactly is web service composition?
Composition refers to the way something is build, the new term at the moment is mash-up which basically means utilising a variety of different services in a composite application. So that functionality of disparate application can be used in one application.
I think your referring to service granularity - which means how much functionality a service exposes. a coarse grained service will expose a whole process as a consumable unit whereas a fine grained service will expose a specific unit of logic from a larger process. Obviously, it is up to the service architects to determine what granularity of service works best in the given environment.
This also, in a way has to do with the style of SOAP message you are using whether it is RPC style or document and that a service should be atomic and not hold external state. Meaning it does not need to know any more information other than that in the SOAP message to perform its function.
Hope this gives you a good starting point. The trouble with service-orientation is that it differs depending on who you read, but the main points stay the same!
Jon
Some web services which are provided for clients are abstract and composition of some smaller web services and it's called web service composition.
Sometimes there are more than one web service in order to use as the mentioned small web services, so we choose them based on QoS (Quality of Service) and many researches have been done on this subject.
Web service composition involves integration of two or more web service to achieve more added value of business functionality. A work flow composer is responsible of aggregating different web services to act as a single service according to functional requirements as well as QoS constrains. BPEL is one of the popular composers uses XML language to perform service composition. Fine-grained services perform single business task and provides higher flexibility and reusability. However, coarse-grained service involves performing complex business functionality leading to lower flexibility