2016-08-30 48 views
2

我正在使用Java8和Spring4.3.1。從Spring啓動運行Java類

我有一個Java/Spring應用程序託管瀏覽器和移動應用程序客戶端訪問的RESTfult服務。其次,我寫了一個聊天服務器來監聽來自客戶端的事件(socket.io)。此聊天服務器正在從類main方法運行。

聊天服務器類有一個main方法,我想運行,並允許在我的Spring應用程序啓動時監聽事件。這可能嗎?

如果我自己運行main,它可以正常工作,但我希望它在啓動加載Spring應用程序的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 
+0

這種感覺就像你在這裏混了一大堆不同的啓動模式。此服務器的目的是通過Web運行嗎?如果沒有,爲什麼servlet?如果是這樣,明顯的「側面」插座有什麼用? – chrylis

+0

聊天服務器需要運行才能聽取事件。我有一個使用sockets.io發送消息的Ionic移動應用程序。如果我從cmd行運行聊天服務器,它可以很好地工作,但我有另一個Java應用程序運行帶有RESTful服務的Spring。當這個Java應用程序啓動時,我也想啓動聊天服務器。 RESTful服務和聊天服務器都需要通過瀏覽器和移動應用程序進行訪問。 – Richard

+0

也許我正在接近這個錯誤,但我在這裏提供建議。 – Richard

回答

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> 

您可能需要排除的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-deployment-test-wildfly

+0

謝謝,我會試試看。 – Richard

+0

亞歷克斯,這是一個從我的退出Java應用程序單獨的戰爭?最好我想從同一個應用程序運行聊天服務器。我嘗試在我現有的Spring應用程序中創建'SpringBootApp'類,並且Spring Boot啓動,但是接着出現以下錯誤:'服務jboss.undertow.deployment.default-server.default-host./jbosswildfly:java.lang中的StartException。 RuntimeException:java.lang.ClassCastException:org.apache.tomcat.websocket.server。WsServerContainer無法轉換爲io.undertow.websockets.jsr.ServerWebSocketContainer'和'New missing/unsatisfied dependencies:' – Richard

+0

更新了答案 – alexbt

相關問題