Can't get #Singleton to do anything - java

Running on JBoss AS7, I have this:
import javax.inject.Singleton;
#Singleton
public class Connections {
private final List<AtmosphereResource> connections = new ArrayList<AtmosphereResource>();
public void add(AtmosphereResource event) {
connections.add(event);
}
}
and this:
import javax.inject.Inject;
public class PubSubAtmosphereHandler extends AbstractReflectorAtmosphereHandler {
#Inject
private Connections connections;
#Override
public void onRequest(AtmosphereResource event) throws IOException {
[...]
connections.add(event); // <---
}
NPE on the designate line. After reading countless pages and examples, this is one of the ways that is repeated dozens of times, yet it doesn't work. I have the empty beans.xml, placed in my WEB-INF. What am I missing here ?

After some research, it turns out that Atmosphere provides (currently broken) hooks for this kind of functionality. That means it IS possible to simply use your normal annotations and have it work with Atmosphere, despite the 'foreign' instantiation.
If you know where your code will be deployed, you can simply overwrite the default noop InjectorProvider class from Atmosphere by having a class with the same name in the same package and having it provide the proper Injector.
I am adding code for JBoss AS7 at the end of this answer, as well as links to code that is supposed to work on Google Guice and Spring, which I have not tested myself.
If you need your code to work on multiple platforms, you will probably need to figure out how to detect what you are running on and then return the appropriate Injector. Since I'm not very familiar with Guice and Spring, I'll leave that exercise to the reader.
Dirty 'first draft' code for JBoss AS7 (remember that this has to go into the declared package to overwrite the default noop provider):
package org.atmosphere.di;
import java.util.NoSuchElementException;
import java.util.ServiceLoader;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.InjectionTarget;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class InjectorProvider {
private InjectorProvider() {}
public static Injector getInjector() {
return LazyProvider.INJECTOR;
}
private static final class LazyProvider {
private static final Injector INJECTOR;
static {
Injector injector = new Injector() {
#Override public void inject(final Object o) {
try {
final BeanManager bm = (BeanManager) new InitialContext().lookup("java:comp/BeanManager");
final CreationalContext cc = bm.createCreationalContext(null);
final InjectionTarget it = bm.createInjectionTarget(bm.createAnnotatedType(o.getClass()));
it.inject(o, cc);
cc.release();
} catch (final NamingException e) {
e.printStackTrace();
}
}
};
try {
injector = ServiceLoader.load(Injector.class).iterator().next();
} catch (final NoSuchElementException e) {}
INJECTOR = injector;
}
}
}
Please note that the wrapping code is taken from the Atmosphere code base, and is a very bad way to do Singletons in Java. You probably don't want to use this 'as is' in production.
Guice injector, untested: https://github.com/Atmosphere/atmosphere/blob/atmosphere-1.0.x/extras/guice/src/main/java/org/atmosphere/guice/GuiceInjector.java
Spring injector, untested: https://github.com/Atmosphere/atmosphere/blob/atmosphere-1.0.x/extras/spring/src/main/java/org/atmosphere/spring/SpringInjector.java

What others are trying to say is that the lifecycle of the PubSubAtmosphereHandler must be controlled by the container (aka JBoss).
In other words the container is responsible for creating and initialising instances of PubSubAtmosphereHandler.
If you or your framework creates this object then the injections will not take place.

It might be possible to get BeanManager using jndi. Then you can get the bean from there.
BeanManager bm = initialContext.lookup("java:comp/BeanManager");
Bean<Connections> bean = (Bean<Connections>) bm.getBeans(Connections.class).iterator().next();
CreationalContext<Connections> ctx = bm.createCreationalContext(bean);
Connections connections = (Connections) bm.getReference(bean, Connections.class, ctx);
connections.add(event);

Related

auto scan for guice

I have never used guice before, and I wanted to try it out on an example project with jersey based JAX-RS API backed by a service-bean. I followed this guide: http://randomizedsort.blogspot.de/2011/05/using-guice-ified-jersey-in-embedded.html and was able to bring it to work. My setup is very simple, a JAX-RS resource is invoked via Guice and has a field that is annotated #Inject and injected by Guice:
#Path("configuration")
#Produces(MediaType.APPLICATION_JSON)
#Singleton
public class ConfigurationResource {
#Inject
private ConfigurationService configurationService;
So far so good, everything works like it should, besides following: I am using GuiceServletContextListener for setting things up and have to name each component explicitly:
#WebListener
public class GuiceInitializer extends GuiceServletContextListener{
#Override
protected Injector getInjector() {
return Guice.createInjector(new JerseyServletModule() {
#Override
protected void configureServlets() {
//resources
bind(ConfigurationResource.class);
//services
bind(ConfigurationService.class).to(ConfigurationServiceImpl.class);
// Route all requests through GuiceContainer
serve("/management/*").with(GuiceContainer.class);
}
});
}
}
I find it pretty inconvenient to explicitly name all dependencies. I have worked with standalone jersey before and it's perfectly capable of auto-scanning for resources in defined packages. Also Spring and CDI are capable of mapping implementation to interfaces without need to explicitly name them.
Now the question part:
is there any autoscan extension/setting for guice? I found some on the internet, but it's hard to tell which of them are still useable and uptodate.
is there any other possibility to make configuration of implementations and resources more convenient?
thanks in advance.
Leon
I do not think Guice has built in support for someting like the component-scan of Spring framework. However, it is not difficult to simulate this feature in Guice.
You simply need to write a helper module like the following
import com.google.inject.AbstractModule;
import org.reflections.Reflections;
import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
/**
* To use this helper module, call install(new ComponentScanModule("com.foo", Named.class); in the configure method of
* another module class.
*/
public final class ComponentScanModule extends AbstractModule {
private final String packageName;
private final Set<Class<? extends Annotation>> bindingAnnotations;
#SafeVarargs
public ComponentScanModule(String packageName, final Class<? extends Annotation>... bindingAnnotations) {
this.packageName = packageName;
this.bindingAnnotations = new HashSet<>(Arrays.asList(bindingAnnotations));
}
#Override
public void configure() {
Reflections packageReflections = new Reflections(packageName);
bindingAnnotations.stream()
.map(packageReflections::getTypesAnnotatedWith)
.flatMap(Set::stream)
.forEach(this::bind);
}
}
To component scan a package like com.foo and sub packages for classes carrying #Singleton, use it in this way:
public class AppModule extends AbstractModule {
public void configure() {
install(new ComponentScanModule("com.foo", Singleton.class));
}
}

Guice: Using providers to get multiple instances:

I am trying to learn Guice for dependency Injection using Providers to create multiple instances of an object(Example from getting started guide on Guice website). how should I test this? Please advise.
The following is the module:
package testing;
import com.google.inject.AbstractModule;
public class BillingModule extends AbstractModule {
#Override
protected void configure() {
bind(TransactionLog.class).to(DatabaseTransactionLog.class);
bind(BillingService.class).to(RealBillingService.class);
bind(CreditCardProcessor.class).to(PaypalCreditCardProcessor.class);
}
}
The following is the class under test:
package testing;
import com.google.inject.Inject;
import com.google.inject.Provider;
public class RealBillingService implements BillingService {
private Provider<CreditCardProcessor> processorProvider;
private Provider<TransactionLog> transactionLogProvider;
#Inject
public RealBillingService(Provider<CreditCardProcessor> processorProvider,
Provider<TransactionLog> transactionLogProvider) {
this.processorProvider = processorProvider;
this.transactionLogProvider = transactionLogProvider;
}
public void chargeOrder() {
CreditCardProcessor processor = processorProvider.get();
TransactionLog transactionLog = transactionLogProvider.get();
/* use the processor and transaction log here */
processor.toString();
transactionLog.toString();
}
}
The following is the test class with main():
public class test {
public static void main(String[] args) {
Injector injector = Guice.createInjector(new BillingModule());
BillingService billingService = injector.getInstance(BillingService.class);
billingService.chargeOrder();
}
}
Upon running this, I am expecting the output from the following toString methods to show up but am seeing nothing:
processor.toString();
transactionLog.toString();
What am i missing here?
Please advise,
thanks!
This happens because you just call toString without putting the resulting string anywhere (eg the call to System.out.println)
However providers are not intended to be used like that. You should not call Provider.get yourself: instead require the result of the provider, register your provider and let Guice do its job (you can also annotate methods in your modules with #Provides instead of defining provider classes)
By default providers are called each time a new instance of a certain class is required. Instances are not recycled unless you explicitly request it via using scopes (like the builtin Singleton)

NullPointerException when injecting EJB in JBoss

I'm trying to implement a singleton class that manages properties from my database. However, I'm getting a nullPointerException when I try to inject my database bean. Here's my code:
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ejb.EJB;
import javax.ejb.Stateless;
..
#Stateless
public class Properties {
public static volatile Properties instance;
private static Map<String, String> properties = new HashMap<String, String>();
#EJB(WHAT_TO_DO_HERE)
private DatabasePersisterLocal databasePersister;
public static Properties getInstance() {
if (instance == null) {
synchronized (Properties.class) {
if (instance == null) {
instance = new Properties();
}
}
}
return instance;
}
private Properties() {
System.out.println("starting");
System.out.println(databasePersister); //null
//initializing
}
}
I'm exporting my project as a jar, and later, when I try to get the instance of Properties, I get that a starting printed and then a nullPointerException. When I try to inject the same EJB (DatabasePersisterLocal) from my other project, I get no error. What am I doing wrong?
Regardless the issue about bean injection that you are facing, I would like to clarify that is not posible to implement a Singleton with an EJB at least in the way you are trying to do it.
The reason is that the ejb instances are handled by the container. The EJB's Thread Model specifies that every request will be managed by an available EJB's instance, which was previously instantiated and allocated to an ejb list (pool).
However if you are using EJB 3.1, it is posible to use the annotation #Singleton which indicates to container that has to create just one EJB's instance .

Is there a way to get Guice to fail fast during Guice.createInjector

My project is using Guice as the IOC container responsible for providing dependencies (service classes) to a large graph of objects (mostly singletons). Sometimes if a dependency fails during construction and this dependency is required by many objects, the failure will occur over and over again adding the exceptions to the Guice ProvisionException.
I can understand the rational for this behaviour as it gives a list of all the errors that occur to save fixing issues piece meal. However, I would like to disable this feature and 'Fail Fast', as the repeated failure in this case is resource intensive. Further more the 'ProvisionException' contains a list of the same exception.
I do appreciate that this behaviour is symptomatic (smells) of bad practice in the implementation (i.e. resource intensive object creation), but since the dependencies are abstractions for which anyone can provide implementations and plugin using dependency injection there is little defence against it.
What I would like to know is:-
Is there a parameter that enables Guice to exit Injector creation at the first exception?
Any help would be greatly appreciated.
Edit:
#Test
public void guiceExample()
{
Injector injector = Guice.createInjector(new TestModule());
try{
IAmANeedyObject instance = injector.getInstance(IAmANeedyObject.class);
}
catch (ProvisionException e)
{
assertThat(e.getErrorMessages().size(),Is.is(2));
}
}
This test assets two exceptions have been thrown
import com.google.inject.AbstractModule;
import com.google.inject.Inject;
public class TestModule extends AbstractModule {
#Override
protected void configure() {
bind(IWasDesignedWithHonestIntent.class).to(NastyThrowingExample.class);
bind(IMindMyOwnBusiness.class).to(SomeLucklessObject.class);
bind(IAlsoMindMyOwnBusiness.class).to(SomeEquallyLucklessObject.class);
bind(IAmANeedyObject.class).to(LowSelfEsteem.class);
}
}
interface IWasDesignedWithHonestIntent {}
interface IMindMyOwnBusiness {}
interface IAlsoMindMyOwnBusiness {}
interface IAmANeedyObject {}
#Singleton
class NastyThrowingExample implements IWasDesignedWithHonestIntent {
#Inject
public NastyThrowingExample() throws LongSlowAgonisingDeathException {
throw new LongSlowAgonisingDeathException("I am dying");
}
}
class LongSlowAgonisingDeathException extends Exception {
#Inject
public LongSlowAgonisingDeathException(String message) {
super(message);
}
}
class SomeLucklessObject implements IMindMyOwnBusiness {
#Inject
public SomeLucklessObject(IWasDesignedWithHonestIntent designedWithHonestIntent) {
}
}
class SomeEquallyLucklessObject implements IAlsoMindMyOwnBusiness {
#Inject
public SomeEquallyLucklessObject(IWasDesignedWithHonestIntent designedWithHonestIntent) {
}
}
class LowSelfEsteem implements IAmANeedyObject {
#Inject
public LowSelfEsteem(IMindMyOwnBusiness iMindMyOwnBusiness, IAlsoMindMyOwnBusiness alsoMindMyOwnBusiness) {
}
}
Is there a parameter that enables Guice to exit Injector creation at the first exception?
I'm afraid that don't, it doesn't.
You will have to continue with a code like your example.
You can always suggest this for the Guice team on their Google Code page.

Using Google Guice to inject java properties

I want to use google guice to make properties available in all classes of my application. I defined a Module which loads and binds the properties file Test.properties.
Property1=TEST
Property2=25
package com.test;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import com.google.inject.AbstractModule;
import com.google.inject.name.Names;
public class TestConfiguration extends AbstractModule {
#Override
protected void configure() {
Properties properties = new Properties();
try {
properties.load(new FileReader("Test.properties"));
Names.bindProperties(binder(), properties);
} catch (FileNotFoundException e) {
System.out.println("The configuration file Test.properties can not be found");
} catch (IOException e) {
System.out.println("I/O Exception during loading configuration");
}
}
}
I'm using a main class where I create a injector to inject the properties.
package com.test;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class Test {
public static void main(String[] args) {
TestConfiguration config = new TestConfiguration();
Injector injector = Guice.createInjector(config);
TestImpl test = injector.getInstance(TestImpl.class);
}
}
package com.test;
import com.google.inject.Inject;
import com.google.inject.name.Named;
public class TestImpl {
private final String property1;
private final Integer property2;
#Inject
public TestImpl(#Named("Property1") String property1, #Named("Property2") Integer property2) {
System.out.println("Hello World");
this.property1 = property1;
this.property2 = property2;
System.out.println(property1);
System.out.println(property2);
}
}
Now my question. If my TestImpl creates other classes where I also need to inject properties, and those classes also need to inject properties what is the correct way to do this?
Pass the injector to all subclasses and then use injector.getInstance(...) to create the subclasses?
Instanciate a new injector like
TestConfiguration config = new TestConfiguration();
Injector injector = Guice.createInjector(config);
TestImpl test = injector.getInstance(TestImpl.class);
in all nested classes?
Is there an other approach to make the properties available in all classes?
Pass the injector to all subclasses and then use
injector.getInstance(...) to create the subclasses?
no, by doing this you are defeating the purpose of the dependency injection pattern and also coupling all your implementation to Guice. Your implementations should not interact at all with guice, except through the (now standardized) annotations.
Instanciate a new injector like
TestConfiguration config = new TestConfiguration();
Injector injector = Guice.createInjector(config);
TestImpl test = injector.getInstance(TestImpl.class);
in all nested classes?
no, and this is even worse cause you will end up with multiple injectors, hence multiple contexts which will prevent a proper usage of the scopes.
Ideally, you should only use the injector during the bootstrapping of your application. Of course the way to bootstrap it will largely depend of the application.
Is there an other approach to make the properties available in all
classes?
The properties could be injected the same way you did for TestImpl.
If you want TestImpl to use let say a service which also needs some properties (or other services), just let Guice inject it to TestImpl. Guice is taking care of all the instantiation/wiring. You should only tell Guice "how to proceed", by using the binder, when Guice cannot figure this out itself :
public class TestImpl {
private final String property1;
private final Integer property2;
private final IService service;
#Inject
public TestImpl(#Named("Property1") String property1, #Named("Property2") Integer property2, IService service) {
this.property1 = property1;
this.property2 = property2;
this.service= service;
}
}
}
Library "Governator" provide a configuration mapping feature for guice injection. The approach is different, but load from properties files is available.
https://github.com/Netflix/governator/wiki/Configuration-Mapping
The library Guice configuration can inject for you values from Properties or JSON files to your services.
You can inject from the file application.properties to your service as :
#BindConfig(value = "application", syntax = PROPERTIES)
public class Service {
#InjectConfig
private int port;
#InjectConfig
private String url;
#InjectConfig
private Optional<Integer> timeout;
}
You must simply install the modules ConfigurationModule
public class GuiceModule extends AbstractModule {
#Override
protected void configure() {
install(ConfigurationModule.create());
requestInjection(Service.class);
}
}
guice-config
Usage example:
At first we have a properties file 'config.properties':
test.key=test value
And we want to inject this value like this:
#Inject
#Config( Property.TEST_KEY )
private String injectedValue;
We need to load contents of file 'config.properties' into java.util.Properties and pass it to Config module:
Properties props = new Properties();
props.load(...);
Module configModule = new ConfigModule( props, Property.values() );
... and injecting:
Injector injector = Guice.createInjector( configModule );
TestClass testClass = injector.getInstance( TestClass.class );
String injectedValue = testClass.getInjectedValue();
injected value will be 'test value'...
"Now my question. If my TestImpl creates other classes where I also need to inject properties, and those classes also need to inject properties what is the correct way to do this?"
Rule of thumb: avoid the use of "new" Unless it is absolutely necessary, dont let your Impl Class "create other classes". Instead, tell your TestImpl that when created with guice, it should get the required instances injected.

Categories