eclipse-ee4j / eclipse-ee4j/tyrus

Problem with Injection using CDI 2.0 in Java SE

Open
#683 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
Java
Stars
128
Forks
49
PR merge metrics
No merged PRs in 30d

Description

We're having issues trying to inject classes into the ServerEndpoint using CDI 2.0 in Java Se.
@ Inject results in receiving null.

### Our Usage
We currently have a project that uses two clients which run the same application for redundancy purposes. These communicate using a WebSocket Client and Server Endpoint on both sides. Receiving certain messages results in actions that change setting of the client.

For our test Environment we plan to run both clients in the same vm. For this to work with injection we create an SeContainer for each client.
Beeing able to integration test two instances we cannot use static values at all.
### Problem
Injecting into an Endpoint running on a standalone grizzly Server is not working. The Inject cannot provide an Instance of any Class.

A possibile approach would be the ability to add an own CompomentProveriderService to the Server. That way we should be able to get to the right instance in the Endpoint Class.

Another idea: is there a way to get specified object instances into a ServerEndpoint? That way the SeContainer could be passed to the ServerEndpoint.
### Example

The WebSocker Server is started in the client class where the induvidualWorker instance of the client is. We need a way to get the worker to the Endpoint either by passing it or by using injection. I guess a possibility would be to add an own ComponentProviderService to the Server.
The worker is used to change setting of the specific client and other injected classed that are in the same cdi container as the client.

@Inject
private IndividualWorker worker;

public void startWebSocketServer(int port) {
Server server = new Server("localhost", port, "/ws", null, ClientWebSocketServer.class);
// server.addComponentPRoviderService(MyComponentProviderServic(worker));
try {
server.start();
} catch (DeploymentException ex) {
L.error("not able to Start Server! {}", ex.getMessage());
}
}
ClientWebSocketServer needs the instance of the IndividualWorker from the associated Client. It would be perfekt beeing able to use Inject annotation.

private String ignoreString;

//@Inject would be perfect to inject the individualWorker
private IndividualWorker individualWorker; // <- Need this from the CDI context

@OnMessage
public void onMessage(String message, Session s) {
//receiving a message the individualWorker should do sth
if (individualWorker != null) {
individualWorker.work(message);
}
}
A test would look like this. Each Client gets initialized with it's own SeContainer allowing to use Inject with both clients in the same test. If the WebSocketServerEndpoint of clientOne receives a message the IndividualWorker of clientOne should do sth.

public void testCdi() throws Exception {
//creation of a CDI container for the first App Instance
SeContainerInitializer containerInitOne = SeContainerInitializer.newInstance();
SeContainer containerOne = containerInitOne.initialize();
Client clientOne = containerOne.select(Client.class).get();

//creation of a CDI container for the second App Instance
SeContainerInitializer containerInitTwo = SeContainerInitializer.newInstance();
SeContainer containerTwo = containerInitTwo.initialize();
Client clientTwo = containerTwo.select(Client.class).get();

//define websocket ports for each app
int portAppOne = 1337;
int portAppTwo = 1338;

//start WebsocketServer of both clients
clientOne.startWebSocketServer(portAppOne);
clientTwo.startWebSocketServer(portAppTwo);

//create a WebSocketClient, connect to clientOne's WebSocket Server and send a message
WebSocketClient wsClientOne = new WebSocketClient();
wsClientOne.connect(1337);
wsClientOne.send("message");

//assert -> Worker changed something on clientOne
//assert -> client two is still unchanged
}

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.