我正在使用Java8和Spring4.3.1。
我有一个托管由浏览器和移动应用客户端访问的RESTfult服务的Java / Spring应用程序。其次,我编写了一个聊天服务器,用于监听来自客户端的事件(socket.io
)。此聊天服务器从类main
方法运行。
Chat Server类有一个我想要运行的main
方法,并允许在Spring应用程序启动时侦听事件。这可能吗?
如果我自己运行main
,它可以运行,但是当我启动加载Spring应用程序的Wildfly服务器时,我想让它启动。
还是有更好的方法吗? Chat Server是否应该从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
答案 0 :(得分:0)
您可以在Wildfly
部署聊天服务器,方法是展开SpringBootServletInitializer
,而不是从main
启动聊天服务器。
文档:howto-create-a-deployable-war-file
@SpringBootApplication
public class SpringBootApp extends SpringBootServletInitializer{
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(SpringBootApp.class);
}
//public static void main(String[] args){
// new SpringApplicationBuilder()
// .sources(SpringBootApp.class)
// .run(args);
//}
}
将制作的工件更改为war
,并将其正常部署在wildfly
:
<project>
<packaging>war</packaging>
...
<project>
您可能必须排除使用spring-boot-starter-web
自动导入的tomcat:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
Spring为wildfly提供了一个pom.xml
的Spring Boot示例:spring-boot-deployment-test-wildfly