JMSキューからメッセージを取得し、一部のデータをログファイルに保存する単純なSpring Bootアプリケーションがありますが、Webサーバーは必要ありません。WebサーバーなしでSpring Bootを起動する方法はありますか?
JMSキューからメッセージを取得し、一部のデータをログファイルに保存する単純なSpring Bootアプリケーションがありますが、Webサーバーは必要ありません。WebサーバーなしでSpring Bootを起動する方法はありますか?
回答:
クラスパスにTomcatの依存関係がない場合、Spring Bootには組み込みのTomcatが含まれません。この事実は、EmbeddedServletContainerAutoConfiguration
ソースがここにあるクラスで自分で確認できます。。
コードの@ConditionalOnClass
本質は、クラスでのアノテーションの使用ですEmbeddedTomcat
gs-convert-jar-to-war/complete
Mavenプロジェクトは組み込みのTomcatサーバーを追加spring-boot-starter-tomcat
しますprovided
。これはバグのように感じます。また、stackoverflow.com
サーブレットコンテナなしでスプリングブートを実行したいが、クラスパス上にある場合(たとえば、テスト用)、スプリングブートのドキュメントで説明されているように、以下を使用します。
@Configuration
@EnableAutoConfiguration
public class MyClass{
public static void main(String[] args) throws JAXBException {
SpringApplication app = new SpringApplication(MyClass.class);
app.setWebEnvironment(false); //<<<<<<<<<
ConfigurableApplicationContext ctx = app.run(args);
}
}
また、私はこのプロパティを偶然見つけました:
spring.main.web-environment=false
application.properties
問題なく動作します。
spring.main.web-environment
は廃止されました。
spring.main.web-application-type=none
spring.main.web-application-type=NONE
# REACTIVE, SERVLET
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(MyApplication.class)
.web(WebApplicationType.NONE) // .REACTIVE, .SERVLET
.run(args);
}
}
NONE
-アプリケーションは、Webアプリケーションとして実行してはならず、組み込みWebサーバーを起動してはなりません。REACTIVE
-アプリケーションは、リアクティブWebアプリケーションとして実行し、組み込みのリアクティブWebサーバーを起動する必要があります。SERVLET
-アプリケーションはサーブレットベースのWebアプリケーションとして実行され、埋め込みサーブレットWebサーバーを起動する必要があります。
次のようなものを作成できます。
@SpringBootApplication
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(false).run(args);
}
}
そして
@Component
public class CommandLiner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
// Put your logic here
}
}
依存関係はまだ存在していますが、使用されていません。
最も簡単な解決策。application.propertiesファイル内。前の回答で述べたように、次のプロパティを追加します。
spring.main.web-environment = false
Springブートスターターのバージョン2.0.0の場合、次のプロパティを使用します。
spring.main.web-application-type = none
すべてのプロパティに関するドキュメントについては、次のリンクを使用してください。https: //docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
アプリケーションで(org.springframework.web.client.RestTemplate
REST呼び出しのように)Web機能が必要だが、TOMCATサーバーを起動したくない場合は、POMでそれを除外します。
<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>
プログラムを通じて:
ConfigurableApplicationContext ctx = new SpringApplicationBuilder(YourApplicationMain.class)
.web(WebApplicationType.NONE)
.run(args);
application.propertiesファイルを使用:
spring.main.web-environment=false
application.ymlファイルを通じて:
spring:
main:
web-environment:false
spring.ioサイトの「Getting Started」テンプレートのいずれかを使用したいが、「default」(「gs / spring-boot」)テンプレートに付属しているサーブレット関連のものは必要ない場合は、代わりに、scheduling-tasksテンプレート(pom *にはspring-boot-starterなどが含まれています)を試すことができます。
https://spring.io/guides/gs/scheduling-tasks/
これにより、Spring Bootが提供され、アプリはスタンドアロンとして実行されます(サーブレットやspring-webmvcなどはpomに含まれていません)。これはあなたが望んでいたことです(他の誰かがすでに指摘しているように、JMS固有のものを追加する必要があるかもしれませんが)。
[*私はMavenを使用していますが、Gradleビルドも同様に機能すると想定しています]。
あなたのpomファイルへの依存関係を削除する
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
コトリングについては、ここで私が最近使用したものです:
// src/main/com.blabla/ShellApplication.kt
/**
* Main entry point for the shell application.
*/
@SpringBootApplication
public class ShellApplication : CommandLineRunner {
companion object {
@JvmStatic
fun main(args: Array<String>) {
val application = SpringApplication(ShellApplication::class.java)
application.webApplicationType = WebApplicationType.NONE
application.run(*args);
}
}
override fun run(vararg args: String?) {}
}
// src/main/com.blabla/command/CustomCommand.kt
@ShellComponent
public class CustomCommand {
private val logger = KotlinLogging.logger {}
@ShellMethod("Import, create and update data from CSV")
public fun importCsv(@ShellOption() file: String) {
logger.info("Hi")
}
}
そして、すべてのブートは通常、使用可能なカスタムコマンドを使用したシェルで終了します。
spring-jms
依存関係として(おそらく)追加するだけです。次に、アプリケーションを起動するだけで、サーバーは起動されません。