Bio Tim Ward is a Senior Consulting Engineer and Trainer at Paremus, a co-author of Enterprise OSGi in Action, and has been actively working with OSGi for over six years. Tim has been a regular participant in the OSGi Core Platform and Enterprise Expert Groups, and led the development of several specifications, including OSGi Promises and Asynchronous Services.

Sponsored Content

Software is Changing the World. QCon empowers software development by facilitating the spread of knowledge and innovation in the developer community. A practitioner-driven conference, QCon is designed for technical team leads, architects, engineering directors, and project managers who influence innovation in their teams.

Absolutely. So, Promises, I think a lot of people will have heard of them, at least in passing, as something people typically are using in JavaScript. What Promises are is a representation of some deferred execution. So, something that’s happening asynchronously and you start it over here, it will finish it at sometime over here, but what you’ve got is a holder for the value in the meantime. What you can do is in the style of a Java Future you could block on that and wait for it to finish, or more usefully what you can do is register for a call back that will happen when this thing has completed. And this is something they do a lot in JavaScript because obviously it’s a single threaded model and if you do block, you’ve stopped anyone from running any code in the whole of the web framework, so your browser is just hung and people don't tend to like that. So, that’s at the nub of what a Promise is.

A Promise type is something that you can take and represent a deferred execution but has a mechanism for resolving it, so something where you can set a value in it later, it will have some kind or resolve() or setValue() method. A Future is effectively a view on this deferred execution, but the resolution happens behind the scenes. So a java.util.concurrent.Future is a Future and rather counter intuitively an OSGi Promise is also, academically speaking, a Future because it doesn’t have a method on it to resolve it. On the other hand if you want to use a Java Future and an OSGi Promise in the same piece of code, I think you would quite rightly curse our names if we’d chosen to call it a Future. So, what we have is a Promise because it’s a lot like a JavaScript Promise, which is something that people are used to dealing with, and what OSGi also defines as a deferred type, so the deferred is equivalent to what academics would call a Promise because you can resolve it, so a deferred you create, the person who creates it holds on to it and can later use it to resolve the Promise that is created by the deferred.

In Java, you can’t; well, you can now in Java 8 because they’ve added a CompletableFuture type. However it’s a concrete type and has an awful lot of methods on it, so you can do a lot of very powerful things, but it’s not that easy to use. So one of the things we really wanted to do with OSGi Promises is we wanted to keep them small and separate, so the OSGi Promise API can actually be used independently of OSGi; it has no dependencies on the core framework, has no dependency on any other specification in OSGi. it’s small and isolated and it can actually be implemented on Java versions going way back. So the default implementation uses some Java 5 primitives, because it makes it slightly easier for us to write it, but actually we’ve produced versions that work on Java 1.4 and Java 1.3 and we’re pretty certain that we could do it further back, but nobody has much of the stomach for writing Java 1.1 code.

I think not so much for Java 1.1 and 1.2; but at the moment there is still no real solution for people who are running in embedded frameworks and wanting to use Promises, so something that embedded people often do is they want to send data, say, from an EDGE device and they may be using something like GSM which has horrendous latency and actually Promises are ideal there. But they are stuck using Java 1.4 because the mobile frameworks haven’t advanced beyond that, JavaME sort of stopped in Java 1.4. So, yes, obviously going back further is probably not that useful, but being able to run on Java 1.4 is more useful than you might think and certainly not having to require Java 8 is very useful because despite the fact that it’s great and I love using it, a lot of people aren’t there yet.

Yes. And given that we didn’t want to tie it to the framework, but we do want OSGi framework and other OSGi specs to be able to use it, we don’t want to force an OSGi specification to move to Java 8 just so that they can get some function that actually doesn’t really benefit from having Java 8 baked into the API; it benefits a lot from the addition of lambdas in Java 8 because all of the callbacks are single abstract method types so you can use lambdas to do really elegant stuff when you are composing Promises or mapping them or flat-mapping them or adding fall backs, and all of that you can do with lambdas and it looks great, but equally you don’t need to have lambdas in order to be able to use it, so there is no reason for us to depend on Java 8.

Alex: So these Promises can be used in any Java application, not just OSGi ones.

Absolutely. That was a hard requirement when we were putting it together.

The documentation is public, which includes the JavaDoc, which is pretty much the entirety of the API, it’s very small so people can look at and start playing around with it now, there are already examples coming out of using it and using it with asynchronous services specification. So yes, people can definitely look at it, I would recommend going to the OSGi design repository on GitHub and looking at osgi.org to get the full scoop there. [See https://github.com/osgi/design for more details]

The draft implementation at the moment is in the OSGi build repository; I do work with the EEG [Enterprise Expert Group] but I am not responsible for releasing so I can’t tell you exactly where and when these things are coming out, but one of the things about the draft specification getting a formal nod and a release from the board is these things can be made public.

Alex: Ok, so we’ll look forward to that in the future. Tim Ward, thank you very much.

Is your profile up-to-date? Please take a moment to review and update.

Email Address

Note: If updating/changing your email, a validation request will be sent

Company name:

Keep current company name

Update Company name to:

Company role:

Keep current company role

Update company role to:

Company size:

Keep current company Size

Update company size to:

Country/Zone:

Keep current country/zone

Update country/zone to:

State/Province/Region:

Keep current state/province/region

Update state/province/region to:

Subscribe to our newsletter?

Subscribe to our architect newsletter?

Subscribe to our industry email notices?

You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.

We notice you're using an ad blocker

We understand why you use ad blockers. However to keep InfoQ free we need your support. InfoQ will not provide your data to third parties without individual opt-in consent. We only work with advertisers relevant to our readers. Please consider whitelisting us.