I'm new in Java EE and I have a problem which I have not yet resolved.
I'm working on a Java EE project, which has an EJB project, a project that contains a Web service and another project with a visual interface (Web application).
The problem is when I try to make a dependency injection from one of my web service interfaces to the user interface (web application project). First I would like to clarify that all injections by tags never worked and tried the method: InitialContext.doLookup (name); and this worked great.
I was able to get the implementation of my interface but when I make a call to one of its methods that returns the object, the returned object comes with all its fields empty. And when I test the service from a client created by me this returns the object with all values. I checked beforehand that all classes and objects were implementing the Serializable interface.
My question is: Why do the methods of the service implementation, which I inject in my web application project, return empty objects?
I am using:
Eclipse
Glassfish Server
JAXB(to Web Services)
I apologize for taking so to respond, I would like to tell you that I could solve the problem and would like to make a comment about the problem I had.
I'm working on a project JavaEE as I said. This project contains an EJB module, an EAR project, a web service and a dynamic web project. First I would like to clarify that projects were made by someone else and I'm continuing the work. I may discover that the problem was that they had the persistence within the EJB project, which is not bad but when generating entities rather than keeping them within the project that contains the persistence, the developer moved them to another project called "Utils" and it included in the EAR as another application module. Everything seemed to work well with instances of entities but apparently the project that contains the persistence (EJB) did not have the physical entities in their packages and therefore was not able to build and fill these data when a request was made by injection.
I want to thank you for your concern and quick response.
Regards.
Related
I am following the steps on the tutorial from jBPM documentation to create a business application.
The project is made of the structure:
The KJAR where is the business assets
The service where the project will be running
The model where data structures will be implemented
I have implemented a custom Work Item Handler in the service project, and a question came to my mind: What if I need to reference a Data Object created in business central (via UI) and use this class in the custom work item handler to process it and return to the next tasks?
What I have tried was to add the dependency of the KJAR project on service project pom and it worked fine. But I haven't seen in the documentation nothing related to a solution using this approach.
First, does it makes sense? Second how could I "link" assets created in KJAR project inside the service project?
I haven't been able to find the documentation either.
However the kjar seems to have many of the properties of a java jar. I've successfully managed to define objects in the business central UI, and after uploading them to nexus repository, it is possible to add a standard maven dependency on the jar.
This way, I was able to use the model defined using the business central UI like standard java objects (if you look inside the jar, you can see some familiar java objects as well as the rules definition).
It seems like you were able to achieve the same thing so I am not sure what exactly you are asking :)
If by "link" you mean import into the project then you can do it whatever way you import other dependencies - one way is to add them to the pom.xml - which you seem to have done already. You can get the package and name in the settings section of the business central ui.
If you have your own repository (nexus for example) you can manually upload it there using mvn deploy command.
I'm redesigning an improvement of a platform that already exists developed with Seam framework version 2.1.1.RA2, Java 6 and JBoss 4.2.3.GA and Ivy. I have 4 modules that communicate with each other. I want to modify one of these modules independently of others. I just want to know how to make a simulation of one module using the mock service.
Using beans.xml file you can add alternatives to your project. My suggestion is to use one interface with method definitions and make one bean with real implementation and one with mock implementation. In beans.xml just specify that you want to use class with mock implementation.
http://seamframework.org/Documentation/WhatIsBeansxmlAndWhyDoINeedIt.html
I want to shared common application context in a multi-war Spring application, so i don't need to defined beans again and again. But i don't want to instantiate those beans defined in this common application context in each webapp. I only want to instantiate those beans once and share by all the webapps. Is it possible? Bare me if i'm wrong.
PS: this common application context exists in jar, which i'll import to every webapps.
1, Below article tell us how to share the common application context, but need to instantiate those beans in each webapp.
How to import spring-config.xml of one project into spring-config.xml of another project?
2, Below is another article i just read, i demo it, but still don't get what i want, beans got instantiated twice. Can someone check this section "Why would you want to use this?" in the article, i don't get it, do they have a solution there, can someone help me here, thanks a lot for your time.
http://spring.io/blog/2007/06/11/using-a-shared-parent-application-context-in-a-multi-war-spring-application/
here is the demo source code in second article: https://github.com/jasonluo/ceciic/tree/master/Research/multiple-contexts-sample
Updates
The problem of case2 is because i didn't deploy as a ear file, thanks Deinum for pointing this out, i'm using tomcat, so there is no way to achieve that.
Our solution now is using REST to access the service webapp which running in a separate server.
Don't, there is usually classloader isolation going on to prevent this. Doing this right usually requires a thorough understanding of classloading, Java EE, packaging and your server.
Having that said there is way to do this as outlined in the blog you posted. You have to:
package both WARs into an EAR (this means you have to use and appserver like WildFly AS and can't just use a servlet engine like Tomcat or Jetty)
package both WARs as "skinny" WARs with at least Spring (and all its dependencies) and the shared beans (and all their dependencies) in the lib/ folder of the EAR instead of the WEB-INF/lib folder of the WARs.
Even then it depends on implementation details of the server. AFAIK it is not guaranteed to work under the Java EE specification. For example it likely won't work in GlassFish.
Update
I can't tell if the output of your demo is correct because I couldn't find a build file.
Yes, Tomcat does not support EARs, you could use Apache TomEE which is very similar and supports EARs (I don't know if TomEE does classloading in a way that makes this work). In theory you could also make use of the common classloader in Tomcat but that would be quite a hack.
There is a way to do it, using Spring Dynamic Modules, but it requires OSGi environment, which is quite different from simple Tomcat. Few articles worth reading:
Deploying Spring MVC applications to OSGi
Hello world style example
Blueprint - a further development on DM
Another blueprint documentation reference to Spring
That being said there is not a lot of up to date information about Spring with OSGi, but it's worth a try to achieve just what you said (but of course, with additional performance cost)
I would like to develop a "database based" java desktop application in the following way:
Develop the Data Access Layer (DAL) using JPA (POJOs generated by Netbeans 7.4)
Develop the Business Layer (BL) (my own classes, controllers, etc.)
Develop the Presentation Layer (PL): Graphical User Interfaces (Panels, Frames, Dialogs)
Making the (PL) communicate with the (BL)
I developed the (DAL + BL) in a single Netbeans project (ProjectDBL.jar).
I developed the PL in a separate Netbeans project (ProjectGUI)
I am importing ProjectDBL.jar into ProjectGUI as a compiled library.
I didn't add the EclipseLink libraries to ProjectGUI since they were added in the ProjectDBL.jar.
I didn't add the database driver library to ProjectGUI for the same reasons.
I would like to fully separate between my DAL+BL and my PL. Further database modification (MySQL->SQLServer for example) should not impact all what was done in the PL.
The problem i am facing is a kind of exception raising when i want to invoke any method in the ProjectDBL.jar:
Exception in thread "main" java.lang.NoClassDefFoundError: javax/persistence/EntityNotFoundException
It seems as if the Persistence Unit must not be instantiated by an external jar...
A compiled library doesn't include all the libraries it uses. Otherwize, every jar file would be 50MBs large, and you would end up with commonly used libraries being present several times in your classpath.
You neet to add every library you use, directly or indirectly, to the classpath.
Consider using an Enterprise Application archetype for this.
Your DAL and BL would be contained in the EJB project, and the PL would be in the web project.
This specific case is exactly what this archetype is for, and will ensure that you're including the necessary libraries in each module, not just the compiled classes stripped of their dependancies.
As an aside, when using JPA, if your DAL/BL is encapsulated in EJB classes, you also get Container Managed Transactions, so it's a good idea to segregate classes as you have anyway, because you can take better advantage of JEE's extensive built-in plumbing.
I have a large scale project I am working on at the moment using Eclipse. Normally, as a one man team, these problems would not be an issue, but as our team is not one person we need to be able to break up pieces of the project to be worked on by certain team members.
In simplicity, let's say I have two layers to be separated apart:
1. Each DAO is a separate Java project, to be worked upon individually
2. The web-tier service layer contains all of our service endpoints and must be able to reference all of the DAOs. This layer runs on Tomcat as a dynamic web project, and utilizes Adobe LiveCycle Data Services as the piece that handles creation and management of endpoints.
Now, the issue we are running into is that when we create a DAO and unit test it individually it runs great. But when we reference it into our service project and try to run it we begin to get all kinds of issues related to the fact that we have two different versions of certain jars referenced in and as such we begin to have errors when running the server.
As a result, we know we can solve the issue by pulling the problem jars and ensuring that this is not an issue again in the future, but as I said before this is a large scale project with multiple people working on it and we don't want to be spending our time weeding out dependency issues when under the gun.
We are looking for recommendations on where to proceed for alternative solutions? Our team is new to JavaEE and as such we don't have much of a bearing on what we can use to tie everything together in it, or if it is a viable solution. Should we be looking at turning our DAOs into EJBs and deploying them in an EAR library? If so, where would our service layer lie, and would the service layer be able to reference the DAO classes since the EJB maintains it's own classpath (from what we have read?) Are we looking down the wrong path, or are we completely wrong in our current understanding of JavaEE?
Any assistance is greatly appreciated. We are still in the framework stage of this project and we want to be sure that we will be able to maintain it in the long run.
I second the Maven recommendation. That can add all sorts of sanity to your project structure.
Maven can even generate Eclipse workspaces via mvn eclipse:eclipse
An important clarification on the EJBs note. As of ava EE 6 is you no longer need to separate EJBs from Servlets and can use them together in the very same jar in the war file.
So understand from that that using EJBs or not no longer has any impact on packaging or classloaders as it once did. These are now separate decisions. EARs and classloader separation should now be viewed as a feature you might want to use should you want classloader separation and the complexity it brings. Most applications simply do not need that and are more than fine with just a war file containing servlets, ejbs, jpa entities, cdi beans, jaxrs services and whatever else you need. You are free to decide how you want to separate them or if you want to bother separating them at all.
EJBs do make great DAOs due to transaction management, something you don't get from plain Tomcat but can be made available in Tomcat via TomEE and works fine in Eclipse. You should consider EJBs for that reason, not for dependency reasons.
Side note, as you're new to Java EE, you might find this helpful:
http://openejb.apache.org/examples-trunk/index.html
In order to have things organized when working with Java EE in teams of 1+ people I could suggest:
Use Maven to manage your build process and library dependencies.
Maven has a small learning curve, but once you grasp it you will be grateful. By using Maven you no longer depends on Eclipse to manage your classpath.
A thing about it that I think is really helpful when working in teams is the install feature. Suppose you are woking on the version 1.0 of an EJB module, say core-ejb-module-1.0, and you've got it to a stable state and want everyone working in the project to refer to it from now on.
You then run a maven command like this on it: mvn clean package install
Maven will clean this module, compile it, run tests, create the jar and then install it to a repository that you define. Could be any computer in your company.
Now you may tell the guys working on other projects to update this dependency version on their .pom file and in the next build they run, before compiling, maven will download this library and then use it. Really neat. No more classpath hell.
(There are other ways to always automatically refer to the latest library as stated in this post, but there are some caveats. Anyway it's just an example.)
Use JPA/EJB instead of DAO Pattern.
Some people say DAO meaning any sort of data access, others really mean that they use the DAO Pattern to access objects. If that is your case, you no longer need to use it when using JPA. (At least for most common scenarios).
In my case, I have a generic EntityService which is capable of doing CRUD operations on any Entity and has a centralized query management. Then every EJB's that should perform database related operations may inject this guy and do its job.
As a suggestion, with Maven, you project could be organized as such:
core project structure
core (The pom root)
core-ejb-module (Includes all generic EJB's, like the EntityService for instance.)
core-jpa-module (Includes all JPA generic definitions, like Interfaces, MappedSuperclasses and such.)
core-jsf-module (Includes all JSF generic definitions, like abstract controllers, generic converters and wrappers for FacesContext, etc..)
Now that you have a core generic module setup, you could create:
an application structure
app (The pom root)
app-ear-module (Includes all other modules in this application. Shared jars goes in the ear /lib folder, so all other modules could reference to them.)
app-ejb-module-a (Includes EJB's for the business layer of your application. It uses the core-ejb-module)
app-ejb-module-b (You may have lots of ejb modules. You may even have a project that contains only ejb modules. Other apps will declare their dependency on them via Maven.)
app-jpa-module (Contains definitions for JPA Entities that represents you database tables. Depends on the core-jpa-module)
app-web-module (Holds the pages, Controllers and Converters for this application.)
I think you got the idea. Things tend to be loosely coupled and you may organize your projects as you like.
This is just a simple example to illustrate. I didn't explained a lot about Maven but if you're interested I think it will help you indeed.
I hope it gives you some ideas and may help you in any way.
[]'s
If you can run all the sub-components using the same set of dependencies, you may find it helpful to migrate to a Maven build.
With Maven, you can define a top-level project that manages all the 3rd party dependency versions in one place, so all modules are built, tested and deployed against the same library versions. You are also likely to find Maven a good fit for the multi-module approach you have adopted, as it ensures that a project is rebuilt correctly if one of its dependencies changes.
You would still be able to use dynamic web projects as before; Eclipse will automatically deploy the DAOs as part of the service project (IIRC you need to characterise the DAOs as utility modules).
If you do go down the EJB root, you are correct that each EAR will get its own class-loader, and can therefore use a varying set of dependencies. However, in your position I would tend to look at improving your dependency management first - it'll probably be cheaper and easier.