Jetty 12: внедрение бизнес-объекта в конечную точку сервера без аннотацийJAVA

Программисты JAVA общаются здесь
Ответить Пред. темаСлед. тема
Гость
 Jetty 12: внедрение бизнес-объекта в конечную точку сервера без аннотаций

Сообщение Гость »


У меня есть небольшое приложение, в котором определена @ServerEndpoint (для веб-сокетов).
Теперь я хотел бы внедрить бизнес-объект, который выполняет некоторую логику, чтобы я мог с ним работать.
Здесь я создаю сервер причала и говорю ему использовать мой EventSocket.class, который содержит @ServerEndpoint("/").

Код: Выделить всё

public JettyServer(Application application) {
this.application = application;
this.server = new Server(8080);
EventSocket.application = application; //so nasty, need to figure out how to inject correctly in constructor

ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
servletContextHandler.setContextPath("/");
server.setHandler(servletContextHandler);

// Add jakarta.websocket support
JakartaWebSocketServletContainerInitializer.configure(servletContextHandler, (context, container) ->
{
container.addEndpoint(EventSocket.class);
});

// Add default servlet (to serve the html/css/js)
// Figure out where the static files are stored.
URL urlStatics = Thread.currentThread().getContextClassLoader().getResource("index.html");
Objects.requireNonNull(urlStatics, "Unable to find index.html in classpath");
String urlBase = urlStatics.toExternalForm().replaceFirst("/[^/]*$", "/");
ServletHolder defHolder = new ServletHolder("default", new DefaultServlet());
defHolder.setInitParameter("resourceBase", urlBase);
defHolder.setInitParameter("dirAllowed", "true");
servletContextHandler.addServlet(defHolder, "/");
}
Each time a request comes in, jetty creates a new instance of this EventSocket object. Now i need to somehow modify jetty that it uses a special configuration when it wants to create that object, because i need to define a listener in this EventSocket.java.
The part where i need this listener in EventSocket:

Код: Выделить всё

@OnMessage
public void onMessage(String message) {
System.out.println(message);
String command = message.split(":")[0];
switch (command) {
case "x": listener.gotCalledWith(message); break;
}

listener is not defined, it should be passed to the default constructor of EventSocket, but thats not possible because i have no clue how to configure jetty to use another constructor. I already created https://github.com/jetty/jetty.project/issues/11502 but no answer yet. Any hint/idea is appreciated!


Источник: https://stackoverflow.com/questions/781 ... nnotations
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «JAVA»