简体   繁体   中英

Dependency Injection Not working for @ClientEndpoint java

Is there any way to enable cdi within this @ClientEndpoint class (still using annotations as opposed to programmatic endpoint classes)? I am using wildfly 14 and java 8.

Here is the code that creates the session, passing the classname to the "createConnection" method:

@ApplicationScoped //TODO move this to be request scoped
public class SessionProducer {

@Produces
public Session getSession(InjectionPoint ip) {
        SessionAnnotation annotation = ip.getAnnotated().getAnnotation(SessionAnnotation.class);
        if(annotation != null) {
            Class clazz = annotation.clazz();
            String url = annotation.serverURL();
              WebSocketContainer webSocketContainer = ContainerProvider.getWebSocketContainer();
                try {
                    return webSocketContainer.connectToServer(clazz, new URI(url)); //<----------this is the line that uses the annotated class (clazz is a reference to the class)
                } catch (DeploymentException | IOException | URISyntaxException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
        return null;    
    }   

/**
 * The destroy/disposer metho for the session
 * @param session
 */
public void close(@Disposes Session session) {

       try {
        session.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    }
}

Here is the annotated endpoint class:

@ClientEndpoint
public class CryptoCompareWSClient {

@Inject
@CryptoCompare
private Event<String> cryptoCompareEvent; //<--------this is always null, no cdi happens

public CryptoCompareWSClient() {
    System.out.println("constructor");
    //cryptoCompareEvent = new Event();
}

@PostConstruct
public void init() {
    System.out.println("post construct"); //<---------this never gets called
}

@OnOpen
public void open(Session session) {
    //session.getAsyncRemote().sendText("SubAdd: { subs: ['0~Poloniex~BTC~USD'] }"  /*"test"*/);
    System.out.println("opened");
}


 @OnClose
public void close(Session session) {
    System.out.println("Session " + session + " closed");
}

 @OnError
 public void error(Throwable error) {
    System.out.println("Error: " + error.getMessage());
}

 @OnMessage
 public void message(String message, Session session) {
     System.out.println("Message");
     //cryptoCompareEvent.fireAsync(message);
 }
 }

Is there any way to enable cdi in the enabled class?

Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM