How to get started with Java modules? - java

I'm reading about OSGi and JBoss modules. I understand the purpose of modules -- to avoid Jar hell and conflicting classes -- but I don't know where to start if I want to write a new application that uses them. Is there a kernel somewhere that I can start with, that then loads the other modules? I don't want to run Eclipse or Wildfly; I want to start fresh with my own app. Hello world for modules.
Edit: I'm using IntelliJ, and for several reasons I really don't want to switch to Eclipse. So I can't do much with answers that require I install Eclipse plugins.

OSGi would be the first choice. JBoss Modules is still much more an internal JBoss thing than a general purpose modularity solution.
To start with an OSGi application, take a look at Bndtools. This is an Eclipse plugin to make OSGi development easy. From there you can create a "run configuration" which is the definition of your application. From there you can also export the application to a single executable JAR to distribute your app.
This is a nice video to get you started.
Also, for more high level components (REST, databases, scheduling etc.) take a look at Amdatu.

Try starting with OSGi enRoute: http://enroute.osgi.org/. This is a project, still in development, to demonstrate using OSGi.

Related

Which .JAR do I use to embed Jetty?

I'm at the point in my application where I would like to have an HTTP Server embedded into my project that updates the page in real-time using AJAJ(Similar to AJAX). However, I have no idea where to begin and the amount of tutorials on this subject are fairly limited, so I decided to go with a name that I've heard quite a few times before, Jetty
So, I downloaded Jetty and read through some documentation, and I'm staring at their beginner tutorial asking myself, "Which one of these f*kin jars do I use?" There's like 9,001 of them. Not to mention that there's like 1200 folders that all contain 1500 more jar files each.
Okay, I'm over exaggerating, but take a look.
It's fairly, uhm... confusing. This is much different than most libraries that are a single jar file, this is just... insane.
Anyway, I'm trying to figure out what all I need to be able to use JQuery, AJAX(AJAJ), and basic HTML features.
I'd suggest you to start with this simple tutorial and jetty-all jar
Embedding Jetty Webinar recording
Embedding Jetty docs
jetty-all different versions downlad
To followup on Gas's answer.
jetty-all doesn't have 100% of Jetty.
It used to, hence the name.
However, today its impossible to have 100% of jetty, as many components can conflict with each other.
If you use maven, or gradle, or ant+ivy, then you'll likely want to depend on:
org.eclipse.jetty:jetty-webapp
org.eclipse.jetty.websocket:javax-websocket-server-impl
let the transitive nature of those build tools pull in the rest.
This would get you "started" easily enough.
There are also plenty of example projects that use embedded jetty.
See:
Embedded Jetty: with JSP enabled
Embedded Jetty: with various WebSocket configurations
Embedded Jetty: using Servlet 3.0 features
Embedded Jetty: using Servlet 3.1 features
Embedded Jetty: various Logging configurations
Some use 100% embedded jetty (without a war file, or WEB-INF, or web.xml), some use a war file built elsewhere.
Jetty uses maven so it can participate in the global central artifact repository, and that we have 2 developers on Jetty that are also developers on Maven.
If you want to manage the dependencies yourself, then you will need to know intimately the purpose and role/purpose/relationship/requirements of every jar file that you are going to add into your project. (and answering that is way out of scope for stackoverflow)
You have many build tool options to make managing the dependencies easier:
Apache Maven
Gradle/Grails
Apache Buildr
Apache Ivy (an add-on for Apache ant)
Groovy Grape
Scala SBT (for working with Scala on top of Java)
Leiningen (for working with Clojure on top of Java)
Maven isn't required, you could use any of the above tools.
Tip: Maven and Gradle are the best integrated in various IDEs (like Eclipse IDE and IntelliJ)

Eclipse tooling for osgi bundle restart on workspace change

We develop a huge set of OSGi bundles based on Equinox and start it within Eclipse with a "OSGi Framework" launch configuration. The start of the framework with all necessary bundles and data takes 1-2 minutes which is the reason why we used DCEVM and JRebel to hot-deploy changed classes.
Now with Java 8 these methods do not work properly any more (DCEVM is available for Java 7 only, and JRebel has too many limitations - freezing debug sessions, no support for added instance fields etc). Since our application is quite modular, I imagine an Eclipse function (via plugin?) that monitors the workspace for class changes and restarts the affected bundles after the incremental build is finished.
Are you aware of some tooling that might help for this task?
Bndtools provides such an environment. It is also superior to the eclipse plugin perspective in a lot of other ways. For example it has great support for declarative services.
DCEVM supports java8 now. Lets look at https://github.com/dcevm/dcevm
If you need hotswap support Eclipse RCP/OSGI you can use https://github.com/HotswapProjects/HotswapAgent It has module for Eclipse OSGI.

How do you break a large java application into components?

I am writing a java web application using spring, hibernate and mysql. The applications is getting larger so I want to break it into smaller parts e.g. smaller projects, components or which ever way possible. For example I have login and image uploading functionality, I don't want both of these to be in a single code base or project. I want to be able to use these separately almost like separate services independent of each other. Is there a way of doing this e.g. convert the image uploading functionality into a jar and then reference this jar in the actual project? How are the Enterprise projects split?
Hope it make makes sense and please advice if I am going the right direction as it is my first project? It would be nice if someone can point me to a mini example or recommend reading.
thanks in advance
Try with OSGi. If you are using Spring, then declarative services and possibly blueprint container in OSGi specs are what you need to leverage what you have done until now.
Try reading OSGi in Action by Richard S. Hall, Karl Pauls, Stuart McCulloch, and David Savage to get an insight in this technology.
OSGi is a modularazied approach for java software development that enforces loosely coupled services creation. Quoting from the OSGi alliance website:
OSGi technology is a set of specifications that defines a dynamic component system for Java. These specifications reduce software complexity by providing a modular architecture for large-scale distributed systems as well as small, embedded applications.
Hibernate supports OSGi as it can be seen here. As for the OSGi implementation, I would recommend Equinox, but many other valid OSGi implementations exist. Since your project is a web application, you could check also Virgo
Virgo from EclipseRT is a completely module-based Java application server that is designed to run enterprise Java applications and Spring-powered applications with a high degree of flexibility and reliability
and in particular,
supports vanilla WAR files, with all their dependencies in WEB-INF/lib, and Web Application Bundles, that import their dependencies via OSGi manifest metadata, via the reference implementation of the OSGi Web Container specification, based on embedded Apache Tomcat and configured using the standard Tomcat server.xml
I ended up using maven modules and maven dependency management capability. I created a separate maven project and kept adding maven modules as needed e.g. created a module for image uploading, one module for common libraries and so on.
Each of these modules are then packaged to jar files. I then import each one of these jar files using maven dependency to my main web project.
It seems to have worked great so far. Hope this is helpful for someone else, too.

How to build desktop applications with Apache Karaf/OSGi?

Well im into building OSGi application for a while, but i can't really find some tutorials which show the way to build Desktop Application ( in any languages ) with OSGi/Karaf.
I want to use karaf because it can then wrap Felix/Equinox. Most of Tutorials i found are about Enteprise Application(WEB). So is there any tutorials, which talk about building desktop application based on OSGi/Karaf?
Here is an example of a GUI application project that uses Apache karaf:
https://bitbucket.org/lorainelab/igb-fx
Using karaf to run a gui-based desktop application decreases development time. A developer makes a change to a bundle, builds the bundle, and then uses karaf shell commands to reload just that one bundle into the running application. There is no need to restart the entire application to view the effects of the new code. This is why were are using it.
We are using OSGi because we want greater modularity in the code base and because we want to support dynamically loaded "Apps" developed by 3rd party developers who write against our genome browser API.
Sorry the main intend right now for Karaf is to be used as a Server, though it still should be possible to start some guis on top. As your already familiar with building such bundles I'm sure you most probably could use just the minor version of Karaf and place your bundles in it. I have to admit I never done this before, so I'm not really sure about it.
First of all OSGI is a component framework, it also leverages a feature missing in Java since its inception - runtime versioning. If you really need it (OSGI), it doesn't depend wether you develop a WEB or desktop application. In general I would recommend you to take a look at this documentation:
http://fusesource.com/docs/esb/4.2/deploy_osgi/Build-ModifyMaven.html
In that link you'll find how to convert an existing Maven project to bundle, check further chapters - there's info how-to deploy jars in e.g. Karaf (Karaf is an OSGI container)
Hope, this helps

Managing a Large OSGi Application

I have a large, growing OSGi application with a number of bundles. I am curious to know the best way to manage this type of application. Currently, I am using Eclipse and Maven, but although this is great for building bundles (via maven-bundle-plugin), as of now it has not been easy to manage the entire application.
What I would like to do is either have ONE run configuration or ONE pom.xml that can be launched and the entire application/project be built and launched. Also, I would like to have something that would be good for debugging.
I have heard of PAX Construct and have it installed in Eclipse, but so far it has been of little help (maybe I'm not using it correctly).
I am sure there are people out there with large OSGi applications that are being managed correctly. Any advice that could be shared would help tremendously.
Thank you,
Stephen
A run configuration is possible via Pax Runner. It lets you choose OSGi platform implementation, specify profiles (pre-packaged sets of bundles for some role, e.g. web, log, ds, etc.) and has good provisioning support, for instance it can load bundles from Maven repository. As a result, you can have a run configuration like
--platform=felix
--log=INFO
--profiles=scalamodules,ds,config,log
mvn:com.my/bundle/1.0.1-SNAPSHOT#update
# other bundles
In case your application is very large or you have different applications, there a way to create own profiles as well.
Well...
It all deopends on what do You mean by "managing" the application.
For dev time launching, building and debugging - Eclipse IDE should fit the bill just perfectly.
Maven... I can't speak for it, as I've never used it myself.
We have a pretty large eclipse based application (several, actually) and on the dev side of things we are not using anything special besides the Eclipse and it's integrated SCM.
In the cc build server, we also use headless eclipse to do the building and packaging.
Now the setup of the workspace has gone a bit out of hand of late with all the dependencies and intermediate build steps, so we are investigating Buckminster for managing the materialization of target platform and workspace resources.
If that works out, we'll probably move to building with Bucky as well - it sure looks promising.
(I do not have any experience with PAX, but at a glance, it looks promising as well...)
i'm quite new to OSGi but,
wouldn't it be possible to use OBR-service in such a way that
you would have one OBR repository file which needs the bundles
and let the OBR-service figure out the dependencies and populate your OSGIhost for you?
This area I think has very poor support at the moment. OSGI doesn't really define anything about deployment or packaging so its up to other frameworks (e.g. Eclipse) to come up with their own way of doing it.
If you are building an RCP (Eclipse base) application, then the eclipse systems do all this stuff, right down to creating exes etc. However builds are mainly done on the Eclipse workspace, headless builds are trickier. The Tycho project is trying to make this more sensible by joining the Maven and Eclipse build cycles, however it is still focussed on RCP applications rather than generic OSGI.
If you not doing RCP, which is my situation as well, then you probably have to roll your own solution, as I haven't found any general solution. Here's an outline of what we do:
We define one POM project that lists all the bundles that are contained in your application. All this project does is list the references - lets call it the 'bundle-list' project.
Then, we use pax provision to run the project in development mode. This is achieved by making the 'bundle-list' pom the parent of the provisioning pom of the pax project (usually in the 'provision' folder). Then, when you start pax, it uses the list of bundles from that project to start OSGI. The bundle references in the 'bundle-list' project have to be marked as 'provided' scope for this to work.
Then, to create a distribution, we have another project. This project also has the 'bundle-list' project as its parent. This project uses various plugins to create a distribution, including downloading the bundle jars. The distribution includes scripts that start up OSGI, but these are hand written, there's no pax systems here.
This works well for us to keep the list of bundles in one place, but there's still a lot of hand written scripts, and there are issues sharing configuration between the two systems - e.g. config files, bundle start levels etc.

Categories

Resources