Let's say I want to create a library which I will use in future projects but I also want to include EJBs in that library referencing other EJBs etc.. That library would also contain simple java classes. What is the best way to do that? How do I define the dependencies in this case? I thought I would define them with annotations. If the user of the library wants to configure other dependencies he will be able to do so by overriding them inside the ejb-jar.xml of his project. Has anyone done something like this in the past? How would you go about it when developing in Eclipse?
Basically my problem is that as far as I can tell if I simply create an ejb-jar which I am going to include in all my projects the ejb container is going to instantiate my e.g. MDBs at deployment time even if I don't need all of the MDBs that are contained inside my library but only some of them.
Is a solution to not define MDBs as EJBs with annotations or inside the ejb-jar.xml but only their dependencies?
What about the session beans? Will they be automatically instantiated even if I don't use them inside a project?
EAR files. Although not very common, you could include your library and its dependencies in an EAR file and distribute that.
What I'd like to do is to distribute the library in its own Jar file along with a documentation of dependencies (e.g.: a Maven' POM file or ivy's xml file). Either way, you'll need a dependency manager.
There is also an option to build a fat JAR file in which all the dependencies are exploded. I don't really like that. If I have to include dependencies, I'd go with EAR files.
Related
I have a web application with different features such as map view, dashboard, report etc. But now, we are planning to split the application in different modules such as map module, dashboard module, etc. to make plug-gable as per the requirement. As all the modules will have their respective htmls, js, controllers, dao layers, how can be these divided as independent modules? Will it be a war or a jar files?
Need a suggestion or example which can help me move forward.
Thanks.
If you have different modules, with independent features. Its possible.
I recommend you, to first, find what features are common to all web-modules, so this common-module, should be installed (as a jar for example) in the library folder of your server.
Then, all the modules could be installed in the webapps of your server (in tomcat is called as webapps).
Important:
You must be careful not to duplicate libraries in each web-module, beacause this would generate conflicts. All your common jars (libraries or your own modules should be installed in the libs folder).
If you are using maven I recommend you to have a parent maven project with all your dependencies included, and then all the modules which needs these dependencies can import it as provided.
Microservices might be your best approach given the requirement you are sharing here. Each module i.e reporting, dashboard etc will be a separate microservice. If you use spring boot, you will end up creating multiple jar files and each jar file can be booted on the VM as a separate process and each one comes with its own container (tomcat). Makes things simple.
If all sub modules of your project are tightly coupled it is very difficult to split it. I suggest you to develop new different projects using reference of your old project. There is no technique to split existing project to different war files.
if you use Maven, you can create a parent with all common dependencies, and in its pom.xml you should define all your modules in <modules> </modules> tag. Be careful about the version of the artifacts, it should be the same version when you reference it in child pom.xml, in the parent tag.
About microservices, they are independent services and on every server is just running a single service. So, if you have multiple modules or if you more than one service on each server, it will be in conflict with MS concept.
I have multiple web applications each using spring-hibernate and other open source libraries and portlets, so basically now each war file includes those jar files. How do I move these jars to a common location so that I don't have to put these in each war file? My jars are places in D:/ directory.
I tried creating modules but no success. e.g. if I added jar
<resources>
<resource-root path="mylib.jar"/>
</resources>
and mylib.jar needs another ABC class. That ABC class is in my WAR class-path. Here I get exception while loading this module. mylib.jar could not find ABC class and throws exception.
If those libraries are reused in several applications, probably the best solution would be to create JBoss modules.
For example, OJDBC library is used in several projects I'm developing. So, I added a new module to JBoss 7: https://community.jboss.org/wiki/CreateAModuleForOracleDatasourceInJBoss711Final (it's just an example).
But you said, that you tried creating modules, but with no luck. What was the problem? Did you get some errors?
EDIT
Answer updated in connection with updated question.
So, if I understood correctly, we can divide your libraries into two categories:
First category is "standard libraries": Spring, Hibernate, Log4j etc. So, these libraries might be added as modules into JBoss AS and reused in every WAR (scope=provided in Maven's dependency).
Any other non-standard libraries (i.e. written by yourself) might be added as modules as well. If these libraries require some other dependencies - these dependencies must be listed in module's XML file, as described in: https://docs.jboss.org/author/display/MODULES/Module+descriptors
Hope this helps at least a bit :)
at time i try to add third party libraries in a EJB-JAR. So far it seems to me, that it is not possible, but i am not sure, and why it is not possible.
I want to create single EJB-JARs and WARs, without put them together in one EAR.
Arjan Tijms shows in Java EE - EAR vs separate EJB+WAR three ways, and to clearify it, i want option 2.
My own research comes mostly to questions with following answers J2EE: How to package 3rd party JARs into an EJB jar?, but i don't want an EAR file.
On the oracle site itself shows following packaging for an EJB-JAR: EJB packaging, which don't have a own lib directory like a WAR. I already tried to put the libary in the root directory (Using eclipse + JBoss 7). My first question is, is it possible to add extra libaries into a EJB-JAR, like in a WAR file? This confuses me, because it is possible to deploy a simple EJB-JAR to a application server, but it seems you cannot add third party libaries. For me a EAR seems mostly "overkill", because it have for me no real advantage and second, if i put a library into a EAR, different EJB-JARs connot use different versions from third party libraries. I want the EJB-JAR complete independent from other EJB-JARs and WARs. I only created a additional simple jar project that contains the interfaces, that have to be implemented, and is added to each EJB project and the WAR project. That brings me to question 2: Have i use to an EAR, if yes, why? It seems to me that i missing something oder don't understand yet.
You can put third party JARs in a lib/ folder in the EAR and add
<library-directory>lib</library-directory>
to your application.xml.
Jars aren't supposed to contain jars.
I would just package the EJBs into a WAR (either directly in WEB-INF/classes or as separate ejb-jars in WEB-INF/lib), put the 3rd party libs into WEB-INF/lib and call it a day. Your approach is pretty limiting. You won't be able to inject your EJBs, and you would have to use a remote lookup.
I run many instances of the same web application under Tomcat and to save memory I copy some of the libraries to Tomcat's lib (tomcat\lib) and shared (tomcat\shared) folders. Libraries that go under lib can be found by both Tomcat and web applications, libraries that go under the shared-folder can only be used by web applications. My web application also need some libraries on the web application level so those libraries goes under WEB-INF\lib.
I have defined in my pom.xml that most libraries have a scope of provided so that they're not copied to WEB-INF\lib, however is there any way to define that I would like some libraries to go under a custom-defined folder; e.g. WEB-INF\lib\tomcat\shared and WEB-INF\lib\tomcat\lib, from where I could mover them manually to Tomcat's corresponding folders on the server?
That is not really the recommended way to package dependencies, as usually the web application would be deployed without furhter modification of the war file.
I think you can archieve what you want using the dependency:copy-dependencies goal, but it would require some configuration. You would have to set includeScope to provided, set the correct outputDirectory and then define the artifacts with the includeGroupIds or includeArtifactIds options.
Make a separate maven project which contains the war file and define the dependencies which should be located in the share-folder as scope: provided. Furthermore create an other separate module which has these dependencies and create an appropriate archive from it via maven-assembly-plugin...
I would like to use the spring framework within an EJB3 project. In detail I would like to use the JDBC template class which should be instantinated from a given data source. When I put the spring.jar to my Jboss lib directoy everything is working fine. But when I put the JAR inside my EAR only there seems to be external dependencies from JDbcTemplate to other libraries. EARs/EJBs classloader try to instantinate the JdbcTemplate and shows me that he can not load the class because of external dependencies. It does not show me which additional JARs I have to put in.
Question: Does some body know which addtional JARs I have to include or even how I can search for depending JARs with external tool. I remember there is a tool which can do this, but I do not know its name anymore. I think something like jarjar etc.
Could anyone help please? Thank you.
This smells like an EAR config problem not an Spring problem. Are you sure that the jar is in the EJB's classpath? You might want to check the MANIFEST.MF file of the EJB's jar to verify this.