Java8 및 Spring4.3.1을 사용하고 있습니다.스프링 시작시 Java 클래스 실행
브라우저 및 모바일 앱 클라이언트가 액세스하는 RESTfult 서비스를 호스팅하는 Java/Spring 애플리케이션이 있습니다. 둘째로, 클라이언트로부터 이벤트 (socket.io
)를 수신하는 채팅 서버를 작성했습니다. 이 채팅 서버는 클래스 main
에서 실행됩니다.
채팅 서버 클래스는 실행하고자하는 main
메쏘드를 가지고 있으며, 스프링 애플리케이션이 시작될 때 이벤트를 기다릴 수 있습니다. 이것이 가능한가?
자신이 main
을 실행하면 작동하지만 스프링 애플리케이션을로드하는 Wildfly 서버를 시작할 때 시작하고 싶습니다.
더 좋은 방법이 있습니까? 채팅 서버가 main
방법으로 실행되고 있지 않아야합니까?
나는 다음과 같은 코드가 있습니다
package com.jobs.spring.configuration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class WebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(AppConfig.class);
ctx.setServletContext(servletContext);
Dynamic dynamic = servletContext.addServlet("rest", new DispatcherServlet(ctx));
dynamic.addMapping("/*");
dynamic.setLoadOnStartup(1);
try {
com.jobs.spring.chat.Server chatServer = new com.jobs.spring.chat.Server();
chatServer.run(null);
} catch (Exception e) {
e.printStackTrace();
}
}
}
및
public class Server implements CommandLineRunner {
private static final String SERVER = "localhost";
private static final Integer PORT = 3700;
@Override
public void run(String... args) throws Exception {
main(args);
}
public static void main(String[] args) {
...
및
얻을 다음과 같은 오류 :18:47:08,142 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 66) MSC000001: Failed to start service jboss.undertow.deployment.default-server.default-host./jbosswildfly: org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./jbosswildfly: java.lang.NoClassDefFoundError: Failed to link com/jobs/spring/chat/Server (Module "deployment.jbosswildfly.war:main" from Service Module Loader): org/springframework/boot/CommandLineRunner
Caused by: java.lang.NoClassDefFoundError: Failed to link com/jobs/spring/chat/Server (Module "deployment.jbosswildfly.war:main" from Service Module Loader): org/springframework/boot/CommandLineRunner
느낀다. 이 서버의 의도는 웹을 통해 실행됩니까? 그렇지 않다면 왜 서블릿인가? 그렇다면 외관상의 "측면"소켓은 무엇입니까? – chrylis
이벤트를 수신하려면 채팅 서버를 실행해야합니다. Socket.io를 사용하여 메시지를 보내는 Ionic Mobile App이 있습니다. 채팅 서버를 cmd 행에서 실행하면 완벽하게 작동하지만 RESTful 서비스로 Spring을 실행하는 다른 Java 애플리케이션이있다. 이 자바 애플 리케이션이 시작될 때, 나는 또한 채팅 서버를 시작하고 싶다. RESTful 서비스와 채팅 서버 모두 브라우저 및 모바일 앱에서 액세스해야합니다. – Richard
어쩌면 내가 잘못 접근하고 있지만 조언을 구하기 위해 여기에 있습니다. – Richard