+ All Categories
Home > Technology > When is 'optional' really optional? - Tim Ward

When is 'optional' really optional? - Tim Ward

Date post: 16-May-2015
Category:
Upload: mfrancis
View: 365 times
Download: 3 times
Share this document with a friend
Description:
OSGi Community Event 2013 (http://www.osgi.org/CommunityEvent2013/Schedule) Lightning Talk ABSTRACT OSGi has an excellent, flexible dependency model and enables loosely coupled interactions between bundles via the service registry. Most of the time this is easy to reason about, but having optional dependencies can sometimes lead to surprising results. This talk will highlight the issues you might come across if you start playing with optional dependencies.
Popular Tags:
46
Copyright © 2005 - 2013 Paremus Ltd. May not be reproduced by any means without express permission. All rights reserved. When is ‘optional’ really optional? Oct 2013 When is ‘optional’ really optional? Tim Ward http://www.paremus.com [email protected] Tuesday, 29 October 13
Transcript
Page 1: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

When is ‘optional’ really optional?

Tim Ward http://[email protected]

Tuesday, 29 October 13

Page 2: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

•Senior Consulting Engineer and Architect at Paremus

• 5 years at IBM developing WebSphere Application Server• Container Implementation experience with Java EE and OSGi, including Blueprint, JPA, EJB

and JTA

•OSGi Specification lead for JPA and Bytecode Weaving

•PMC member of the Apache Aries project

•Previous speaker at EclipseCon, Devoxx, Jazoon, JAX London, OSGi Community Event...

•Author of Manning’s Enterprise OSGi in Action

• http://www.manning.com/cummins

Who is Tim Ward?@TimothyWard

Tuesday, 29 October 13

Page 3: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Optional Services

Tuesday, 29 October 13

Page 4: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Optional Services

•OSGi bundles communicate and collaborate using loosely coupled services

•Making a service dependency “optional” is pretty easyOSGi API

ServiceReference<Foo> ref = ctx.getServiceReference(Foo.class);

if(ref != null) {Foo service = ctx.getService(ref);if(service != null) {

try {...

} finally {ctx.ungetService(ref);

}}

}

Declarative Services API

private Foo fooService;

@Reference(cardinality = OPTIONAL)public synchronized void setFoo(Foo service) {

fooService = service;}

public synchronized void unsetFoo(Foo service) {fooService = (fooService == service) ? null :

service;}

Tuesday, 29 October 13

Page 5: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Difficulties with Optional Services

• The OSGi API and DS need null checks, which is messy

•Blueprint injects a proxy to the service - No more nulls!

Blueprint code

private Foo fooService;

public void setFoo(Foo service) {fooService = service;

}

Blueprint XML

<blueprint>

<reference interface=com.paremus.Fooid=”foo” availability=”optional”/>

<bean id=”bar” class=”com.paremus.Bar><property name=”foo” ref=”foo”>

</bean>

</blueprint>

Tuesday, 29 October 13

Page 6: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Blueprint is the winner! Or is it?

Tuesday, 29 October 13

Page 7: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Blueprint is the winner! Or is it?

•At first inspection Blueprint looks much simpler

Tuesday, 29 October 13

Page 8: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Blueprint is the winner! Or is it?

•At first inspection Blueprint looks much simpler

•But what happens when there is no backing service?

Tuesday, 29 October 13

Page 9: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Blueprint is the winner! Or is it?

•At first inspection Blueprint looks much simpler

•But what happens when there is no backing service?

•Calling an unbound blueprint service proxy blocks up to the “timeout”. If no service arrives then you get a ServiceUnavailableException

Tuesday, 29 October 13

Page 10: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Blueprint is the winner! Or is it?

•At first inspection Blueprint looks much simpler

•But what happens when there is no backing service?

•Calling an unbound blueprint service proxy blocks up to the “timeout”. If no service arrives then you get a ServiceUnavailableException

• The default timeout is 5 minutes!!!

Tuesday, 29 October 13

Page 11: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Blueprint is the winner! Or is it?

•At first inspection Blueprint looks much simpler

•But what happens when there is no backing service?

•Calling an unbound blueprint service proxy blocks up to the “timeout”. If no service arrives then you get a ServiceUnavailableException

• The default timeout is 5 minutes!!!

•You can decrease the timeout (but not to zero)

Tuesday, 29 October 13

Page 12: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Blueprint is the winner! Or is it?

•At first inspection Blueprint looks much simpler

•But what happens when there is no backing service?

•Calling an unbound blueprint service proxy blocks up to the “timeout”. If no service arrives then you get a ServiceUnavailableException

• The default timeout is 5 minutes!!!

•You can decrease the timeout (but not to zero)

•You can use a ReferenceListener (this is horrible code, and risks deadlock)

Tuesday, 29 October 13

Page 13: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Blueprint is the winner! Or is it?

•At first inspection Blueprint looks much simpler

•But what happens when there is no backing service?

•Calling an unbound blueprint service proxy blocks up to the “timeout”. If no service arrives then you get a ServiceUnavailableException

• The default timeout is 5 minutes!!!

•You can decrease the timeout (but not to zero)

•You can use a ReferenceListener (this is horrible code, and risks deadlock)

•You can inject a ReferenceList (like DS, but with the wrong cardinality)

Tuesday, 29 October 13

Page 14: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Blueprint 1.1 to the rescue

Tuesday, 29 October 13

Page 15: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Blueprint 1.1 to the rescue

•Despite aiming to be “simple” Blueprint 1.0 sucked at optionality...

Tuesday, 29 October 13

Page 16: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Blueprint 1.1 to the rescue

•Despite aiming to be “simple” Blueprint 1.0 sucked at optionality...

•Blueprint 1.1 proposes several fixes:

Tuesday, 29 October 13

Page 17: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Blueprint 1.1 to the rescue

•Despite aiming to be “simple” Blueprint 1.0 sucked at optionality...

•Blueprint 1.1 proposes several fixes:

•Allow users to have an immediate timeout (no service, no waiting)

Tuesday, 29 October 13

Page 18: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Blueprint 1.1 to the rescue

•Despite aiming to be “simple” Blueprint 1.0 sucked at optionality...

•Blueprint 1.1 proposes several fixes:

•Allow users to have an immediate timeout (no service, no waiting)

•Still have to cope with a ServiceUnavailableException

Tuesday, 29 October 13

Page 19: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Blueprint 1.1 to the rescue

•Despite aiming to be “simple” Blueprint 1.0 sucked at optionality...

•Blueprint 1.1 proposes several fixes:

•Allow users to have an immediate timeout (no service, no waiting)

•Still have to cope with a ServiceUnavailableException

•Allow users to specify a “default” service implementation

Tuesday, 29 October 13

Page 20: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Blueprint 1.1 to the rescue

•Despite aiming to be “simple” Blueprint 1.0 sucked at optionality...

•Blueprint 1.1 proposes several fixes:

•Allow users to have an immediate timeout (no service, no waiting)

•Still have to cope with a ServiceUnavailableException

•Allow users to specify a “default” service implementation

• This can be a nice way to avoid branches in your code

Tuesday, 29 October 13

Page 21: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Blueprint 1.1 to the rescue

•Despite aiming to be “simple” Blueprint 1.0 sucked at optionality...

•Blueprint 1.1 proposes several fixes:

•Allow users to have an immediate timeout (no service, no waiting)

•Still have to cope with a ServiceUnavailableException

•Allow users to specify a “default” service implementation

• This can be a nice way to avoid branches in your code

•Allows users to use a “null proxy” as the default implementation

Tuesday, 29 October 13

Page 22: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Blueprint 1.1 to the rescue

•Despite aiming to be “simple” Blueprint 1.0 sucked at optionality...

•Blueprint 1.1 proposes several fixes:

•Allow users to have an immediate timeout (no service, no waiting)

•Still have to cope with a ServiceUnavailableException

•Allow users to specify a “default” service implementation

• This can be a nice way to avoid branches in your code

•Allows users to use a “null proxy” as the default implementation

•Avoids making you an API provider

Tuesday, 29 October 13

Page 23: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Is Service Optionality really ‘optional’

Tuesday, 29 October 13

Page 24: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Is Service Optionality really ‘optional’

• The optionality so far has made an implicit assumption

Tuesday, 29 October 13

Page 25: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Is Service Optionality really ‘optional’

• The optionality so far has made an implicit assumption

• The API was not optional!

Tuesday, 29 October 13

Page 26: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Is Service Optionality really ‘optional’

• The optionality so far has made an implicit assumption

• The API was not optional!

•OSGi does a great job of managing dependencies

Tuesday, 29 October 13

Page 27: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Is Service Optionality really ‘optional’

• The optionality so far has made an implicit assumption

• The API was not optional!

•OSGi does a great job of managing dependencies

•Automatic dependency provisioning

Tuesday, 29 October 13

Page 28: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Is Service Optionality really ‘optional’

• The optionality so far has made an implicit assumption

• The API was not optional!

•OSGi does a great job of managing dependencies

•Automatic dependency provisioning

•Prevent resolution unless dependencies are satisfied

Tuesday, 29 October 13

Page 29: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Is Service Optionality really ‘optional’

• The optionality so far has made an implicit assumption

• The API was not optional!

•OSGi does a great job of managing dependencies

•Automatic dependency provisioning

•Prevent resolution unless dependencies are satisfied

•Optional dependencies don’t have the same guarantees!

Tuesday, 29 October 13

Page 30: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Optional Packages

Tuesday, 29 October 13

Page 31: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Handling Package Optionality

Tuesday, 29 October 13

Page 32: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Handling Package Optionality

•Having optional package imports makes OSGi more like a normal classpath

Tuesday, 29 October 13

Page 33: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Handling Package Optionality

•Having optional package imports makes OSGi more like a normal classpath

•You need to be defensive - ClassNotFoundException could happen!

Tuesday, 29 October 13

Page 34: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Handling Package Optionality

•Having optional package imports makes OSGi more like a normal classpath

•You need to be defensive - ClassNotFoundException could happen!

•Unusually for OSGi the factory pattern can be helpful

Tuesday, 29 October 13

Page 35: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Handling Package Optionality

•Having optional package imports makes OSGi more like a normal classpath

•You need to be defensive - ClassNotFoundException could happen!

•Unusually for OSGi the factory pattern can be helpful

•Select the implementation with or without the dependency as appropriate

Tuesday, 29 October 13

Page 36: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Handling Package Optionality

•Having optional package imports makes OSGi more like a normal classpath

•You need to be defensive - ClassNotFoundException could happen!

•Unusually for OSGi the factory pattern can be helpful

•Select the implementation with or without the dependency as appropriate

•Blueprint can use factories to create managed beans

Tuesday, 29 October 13

Page 37: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Handling Package Optionality

•Having optional package imports makes OSGi more like a normal classpath

•You need to be defensive - ClassNotFoundException could happen!

•Unusually for OSGi the factory pattern can be helpful

•Select the implementation with or without the dependency as appropriate

•Blueprint can use factories to create managed beans

• It can be useful to isolate dependent code in a handler class

Tuesday, 29 October 13

Page 38: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Handling Package Optionality (2)

Tuesday, 29 October 13

Page 39: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Handling Package Optionality (2)

•Optional packages can be very hard to manage

Tuesday, 29 October 13

Page 40: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Handling Package Optionality (2)

•Optional packages can be very hard to manage

•Having multiple optional dependencies can explode complexity

Tuesday, 29 October 13

Page 41: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Handling Package Optionality (2)

•Optional packages can be very hard to manage

•Having multiple optional dependencies can explode complexity

• Effectively you’re back to the complexity of a Java Classpath!

Tuesday, 29 October 13

Page 42: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Handling Package Optionality (2)

•Optional packages can be very hard to manage

•Having multiple optional dependencies can explode complexity

• Effectively you’re back to the complexity of a Java Classpath!

•Possible approaches to consider:

Tuesday, 29 October 13

Page 43: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Handling Package Optionality (2)

•Optional packages can be very hard to manage

•Having multiple optional dependencies can explode complexity

• Effectively you’re back to the complexity of a Java Classpath!

•Possible approaches to consider:

•Don’t make the API optional, have two bundles, one with the dependency and the other one without

Tuesday, 29 October 13

Page 44: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Handling Package Optionality (2)

•Optional packages can be very hard to manage

•Having multiple optional dependencies can explode complexity

• Effectively you’re back to the complexity of a Java Classpath!

•Possible approaches to consider:

•Don’t make the API optional, have two bundles, one with the dependency and the other one without

• If all you care about is ease of use then repackage the API

Tuesday, 29 October 13

Page 45: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

Handling Package Optionality (2)

•Optional packages can be very hard to manage

•Having multiple optional dependencies can explode complexity

• Effectively you’re back to the complexity of a Java Classpath!

•Possible approaches to consider:

•Don’t make the API optional, have two bundles, one with the dependency and the other one without

• If all you care about is ease of use then repackage the API

•Keep a careful internal dependency structure

Tuesday, 29 October 13

Page 46: When is 'optional' really optional? - Tim Ward

Copyright © 2005 - 2013 Paremus Ltd.May not be reproduced by any means without express permission. All rights reserved.

When is ‘optional’ really optional? Oct 2013

• For more about OSGi...

•Specifications at http://www.osgi.org

• Enterprise OSGi in Action

• http://www.manning.com/cummins

Questions?

Thanks!

http://[email protected]

Tuesday, 29 October 13


Recommended