Created
January 24, 2017 23:09
-
-
Save fabriziocucci/5660af43ebeb7ec04f20ed293cd5676f to your computer and use it in GitHub Desktop.
@Immediate binding fail after Container#reload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.fabriziocucci; | |
import javax.inject.Inject; | |
import javax.ws.rs.GET; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.client.ClientBuilder; | |
import javax.ws.rs.core.Feature; | |
import javax.ws.rs.core.FeatureContext; | |
import javax.ws.rs.core.UriBuilder; | |
import org.glassfish.hk2.api.Immediate; | |
import org.glassfish.hk2.api.ServiceLocator; | |
import org.glassfish.hk2.utilities.ServiceLocatorUtilities; | |
import org.glassfish.hk2.utilities.binding.AbstractBinder; | |
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory; | |
import org.glassfish.jersey.server.ResourceConfig; | |
import org.glassfish.jersey.server.spi.Container; | |
import org.glassfish.jersey.server.spi.ContainerLifecycleListener; | |
// DEPENDENCIES REQUIRED: | |
// | |
// <dependency> | |
// <groupId>org.glassfish.jersey.containers</groupId> | |
// <artifactId>jersey-container-grizzly2-http</artifactId> | |
// <version>2.25.1</version> | |
// </dependency> | |
public class App extends ResourceConfig { | |
private static Container container; | |
public App() { | |
register(ImmediateScopeFeature.class); | |
register(HelloWorldResource.class); | |
register(new ContainerLifecycleListener() { | |
public void onStartup(Container container) { | |
App.container = container; | |
} | |
public void onShutdown(Container container) { } | |
public void onReload(Container container) { } | |
}); | |
register(new AbstractBinder() { | |
@Override | |
protected void configure() { | |
bind(ImmediateGreetingService.class).to(ImmediateGreetingService.class).in(Immediate.class); | |
} | |
}); | |
} | |
private static class ImmediateScopeFeature implements Feature { | |
@Inject | |
public ImmediateScopeFeature(ServiceLocator locator) { | |
ServiceLocatorUtilities.enableImmediateScope(locator); | |
} | |
public boolean configure(FeatureContext context) { | |
return true; | |
} | |
} | |
@Path("/") | |
public static class HelloWorldResource { | |
private final ImmediateGreetingService immediateGreetingService; | |
@Inject | |
public HelloWorldResource(ImmediateGreetingService immediateGreetingService) { | |
this.immediateGreetingService = immediateGreetingService; | |
} | |
@GET | |
public String greet() { | |
return immediateGreetingService.greet(); | |
} | |
} | |
public static class ImmediateGreetingService { | |
public String greet() { | |
return "Hello World!"; | |
} | |
} | |
public static void main(String[] args) { | |
GrizzlyHttpServerFactory.createHttpServer(UriBuilder.fromUri("http://localhost:8080").build(), new App()); | |
String response1 = ClientBuilder.newClient().target("http://localhost:8080").request().get(String.class); | |
assert response1.equals("Hello World!"); | |
container.reload(); | |
String response2 = ClientBuilder.newClient().target("http://localhost:8080").request().get(String.class); // KABOOM! | |
assert response2.equals("Hello World!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment