Mlezi Mlezi - 18 days ago 11
Java Question

Jersey inject instance into Resource

I'm trying to create a Jersey Resource that allows me to reuse an ElasticSearch TransportClient. So I would like to use a single instance of TransportClient over all Resources that require it. So far I've got this:

Resource:

@Path("/request")
public class ConfigurationResource {

private final TransportClient transportClient;

@Inject
public ConfigurationResource(TransportClient transportClient)
{
this.transportClient = transportClient;
}

@GET
@Produces(MediaType.TEXT_PLAIN)
public String AlarmStatus(){
if(transportClient != null)
return "Not NULL! ID: ";
else
return "NULL :(";
}
}


Binding:

public class WebMainBinder extends AbstractBinder {
@Override
protected void configure() {
TransportClient transportClient = null;
try {
transportClient = TransportClient.builder().build()
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
} catch (UnknownHostException e) {
e.printStackTrace();
return;
}
bind(transportClient).to(TransportClient.class);
}
}


Main Application:

@ApplicationPath("service")
public class WebMain extends ResourceConfig {
public WebMain(){
register(new WebMainBinder());
packages(true, "com.eniacdevelopment.EniacHome.Resources");
}
}


Web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">

<servlet>
<servlet-name>com.eniacdevelopment.EniacHome.Application.WebMain</servlet-name>
</servlet>
<servlet-mapping>
<servlet-name>com.eniacdevelopment.EniacHome.Application.WebMain</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
</web-app>


I've also tried using a factory like so:

public class TransportClientFactory implements Factory<TransportClient> {

private TransportClient transportClient;

@Override
public TransportClient provide() {
if(this.transportClient == null){
try {
transportClient = TransportClient.builder().build()
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
} catch (UnknownHostException e) {
e.printStackTrace();
return null;
}
}

return transportClient;
}

@Override
public void dispose(TransportClient transportClient) {
}
}


And then binding it this way:

bindFactory(TransportClientFactory.class)
.to(TransportClient.class).in(Singleton.class);


But no success. I keep on getting Unsatisfied dependencies for type TransportClient with qualifiers @Default.

Help would be much appreciated!

Answer

Alright got it to work:

When I at first tried to deploy the app to glassfish it complained about some guava dependency. I swapped the Guava jar in glassfish/modules with the one maven had installed and got it to deploy. It may have something to do with that.

I decided to drop the whole glassfish stuff and start from scratch. On the jersey introduction page there's this maven archetype that can be isntalled like so:

mvn archetype:generate -DarchetypeArtifactId=jersey-quickstart-grizzly2
-DarchetypeGroupId=org.glassfish.jersey.archetypes -DinteractiveMode=false -DgroupId=com.example -DartifactId=simple-service 
-Dpackage=com.example 
-DarchetypeVersion=2.24

Starting from there helped me out.

Comments