从 Spring 启动运行 Java 类 [英] Run a Java class from Spring startup
问题描述
我使用的是 Java8 和 Spring4.3.1.
I am using Java8 and Spring4.3.1.
我有一个 Java/Spring 应用程序,用于托管浏览器和移动应用程序客户端访问的 RESTfult 服务.其次,我编写了一个聊天服务器来侦听来自客户端的事件 (socket.io
).此聊天服务器从类 main
方法运行.
I have a Java/Spring application hosting RESTfult services accessed by browser and mobile app clients. Second, I have written a Chat Server that listens for events (socket.io
) from the clients. This Chat Server is running from the classes main
method.
Chat Server 类有一个我想要运行的 main
方法,并允许在我的 Spring 应用程序启动时监听事件.这可能吗?
The Chat Server class has a main
method that I want to run, and allow to listen for events when my Spring application starts. Is this possible?
如果我自己运行 main
,它可以工作,但我希望它在我启动加载 Spring 应用程序的 Wildfly 服务器时启动.
If I run the main
myself, it works, but I want it to start up when I start my Wildfly server that loads the Spring application.
或者有更好的方法吗?聊天服务器不应该从 main
方法运行吗?
Or is there a better approach? Should the Chat Server not be running from the main
method?
我有以下代码:
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
推荐答案
您可以在 Wildfly
中部署您的聊天服务器,通过扩展 SpringBootServletInitializer
,而不是从main
.
You could deploy your chat server in Wildfly
, by extending SpringBootServletInitializer
, instead of launching it from a 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);
//}
}
将生成的artifact改为war
,在wildfly
中正常部署:
Change the artifact produced to war
, and deploy it normally in wildfly
:
<project>
<packaging>war</packaging>
...
<project>
<小时>
你可能需要排除tomcat,它是用spring-boot-starter-web
自动导入的:
<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
Spring provided a Spring Boot example of a pom.xml
for wildfly: spring-boot-deployment-test-wildfly
这篇关于从 Spring 启动运行 Java 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!