@ManagedResource和@ManagedOperation声明JMX操作
使程序能够通过JMX接口关闭
@SpringBootApplication
@ManagedResource(objectName=ApplicationShutdown.DEFAULT_OBJECT_NAME)
public class Application {
private static ConfigurableApplicationContext context;
public static void main(String[] args) {
context = SpringApplication.run(Application.class, args);
}
@ManagedOperation
public void shutdown() {
//关闭SpringBoot
int exitCode = SpringApplication.exit(context);
//退出java虚拟机
System.exit(exitCode);
}
}
DEFAULT_OBJECT_NAME必须与前面@ManagedResource中的objectName一致
这里代码就是访问前面Application类中声明的JMX操作
这个类在关闭服务时起作用
public class ApplicationShutdown {
public static final String DEFAULT_OBJECT_NAME = "sample:type=Application,name=application";
public static void main(String[] args) {
if( args.length == 0 ) {
System.out.println("请传入JMX端口号");
}
String url = "service:jmx:rmi:///jndi/rmi://127.0.0.1:" + args[0] + "/jmxrmi";
JMXServiceURL serviceUrl;
try {
serviceUrl = new JMXServiceURL(url);
JMXConnector connection = JMXConnectorFactory.connect(serviceUrl, null);
ObjectName objectName = new ObjectName(DEFAULT_OBJECT_NAME);
connection.getMBeanServerConnection().invoke(objectName, "shutdown", null, null);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (MalformedObjectNameException e) {
e.printStackTrace();
} catch (InstanceNotFoundException e) {
e.printStackTrace();
} catch (MBeanException e) {
e.printStackTrace();
} catch (ReflectionException e) {
e.printStackTrace();
}
}
}
<service>
<id>SpringBootWinSWid>
<name>SpringBootWinSWname>
<description>SpringBootWinSWdescription>
<workingdirectory>%BASE%\workingdirectory>
<logpath>%BASE%\logslogpath>
<logmode>rotatelogmode>
<onfailure action="restart" delay="120 sec"/>
<executable>javaexecutable>
<startargument>-Dfile.encoding=UTF-8startargument>
<startargument>-Dspring.application.admin.enabled=truestartargument>
<startargument>-Dcom.sun.management.jmxremote.port=50201startargument>
<startargument>-Dcom.sun.management.jmxremote.authenticate=falsestartargument>
<startargument>-Dcom.sun.management.jmxremote.ssl=false startargument>
<startargument>-cpstartargument>
<startargument>SpringBootWinSW.jarstartargument>
<startargument>sample.Applicationstartargument>
service>
打包不使用spring-boot-maven-plugin插件
主要是因为使用此插件打包后的jar包和普通的jar包结构不一样,java -cp命令无法识别sample.ApplicationShutdown这个类
pom.xml主要配置
<properties>
<start-class>sample.Applicationstart-class>
<stop-class>sample.ApplicationShutdownstop-class>
<dist.finalname>${project.artifactId}dist.finalname>
<dist.dir>${project.build.directory}/distdist.dir>
<dist.project.id>${dist.finalname}dist.project.id>
<dist.project.name>${dist.finalname}dist.project.name>
<dist.project.description>${dist.finalname}dist.project.description>
<dist.jmx.port>50201dist.jmx.port>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<java.version>1.8java.version>
<skipTests>trueskipTests>
properties>
<build>
<finalName>${dist.finalname}finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-jar-pluginartifactId>
<configuration>
<archive>
<manifest>
<mainClass>${start-class}mainClass>
<addClasspath>trueaddClasspath>
<classpathPrefix>lib/classpathPrefix>
manifest>
<manifestEntries>
<Class-Path>./Class-Path>
manifestEntries>
archive>
configuration>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-resources-pluginartifactId>
<executions>
<execution>
<id>copy-resourcesid>
<phase>process-resourcesphase>
<goals>
<goal>copy-resourcesgoal>
goals>
<configuration>
<outputDirectory>${dist.dir}outputDirectory>
<resources>
<resource>
<directory>src/main/distdirectory>
<filtering>truefiltering>
resource>
resources>
configuration>
execution>
executions>
plugin>
<plugin>
<artifactId>maven-assembly-pluginartifactId>
<configuration>
<appendAssemblyId>falseappendAssemblyId>
<descriptors>
<descriptor>src/main/assembly/windows.xmldescriptor>
descriptors>
configuration>
<executions>
<execution>
<id>make-assemblyid>
<phase>packagephase>
<goals>
<goal>singlegoal>
goals>
execution>
executions>
plugin>
plugins>
build>
window.xml
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>windowsid>
<formats>
<format>zipformat>
formats>
<includeBaseDirectory>trueincludeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>liboutputDirectory>
<excludes>
<exclude>${groupId}:${artifactId}exclude>
excludes>
dependencySet>
dependencySets>
<fileSets>
<fileSet>
<outputDirectory />
<directory>${project.build.directory}directory>
<includes>
<include>*.jarinclude>
includes>
fileSet>
fileSets>
<files>
<file>
<outputDirectory />
<source>src/main/winsw/WinSW.NET2.exesource>
<destName>${dist.finalname}.exedestName>
file>
<file>
<outputDirectory />
<source>${dist.dir}/service.xmlsource>
<destName>${dist.finalname}.xmldestName>
file>
<file>
<outputDirectory />
<source>${dist.dir}/service.exe.configsource>
<destName>${dist.finalname}.exe.configdestName>
file>
<file>
<outputDirectory />
<source>${dist.dir}/安装说明.txtsource>
<destName>安装说明.txtdestName>
file>
files>
assembly>
1.命令cd /d [发布目录]
2.命令SpringBootWinSW.exe install
1.命令cd /d [发布目录]
2.命令SpringBootWinSW.exe uninstall
两种方式
两种方式