---
title: "为WildFly的多个standalone实例创建不同的Windows服务"
author: "chnic"
date: "2016-04-12T00:37:57Z"
tags: []
categories: ["开源软件"]
slug: "wildfly-standalone-windows"
draft: false
lastmod: "2016-04-12T00:37:56Z"
id: 2289518
url: "https://chnic.iteye.com/blog/2289518"
---

之前一篇介绍了如何在WildFly中创建多个基于standalone模式的实例，这一篇来介绍一下如何为这些不同的instances在Windows Server中创建不同的服务。也许你此时此刻很好奇，为什么要为WildFly创建Windows Service。如果你有在Windows Server上维护许多不同WildFly实例的经历，我想你就会对这个问题感同身受。在Windows Server创建为Windows Service有很多好处，其中最明显的就是在Windows Server计划或者无计划重启/宕机之后，你可以很轻松的在你的Windows Server重启之后自动启动那些实例。

 

废话了一堆，我们直接进入正题，在开始之前我假设你已经成功在你的WildFly里搭建了数个基于standalone模式的实例。提到Windows，大部分人都会首先想到exe。没错，一个windows service其实也是一个exe。那么我们应该去哪里找这个exe呢？总不至于自己写吧。其实WildFly已经为我们提供了我们想要的一切。

 

不过寻找exe之前，我们需要先在Windows的环境变量中配置JBOSS\_HOME，指向WildFly的根目录。

![](http://dl2.iteye.com/upload/attachment/0116/5373/0a3d0acb-d128-3626-a9a6-61e33f777bf6.png)  
 

之后进入%JBOSS\_HOME%\\bin\\service目录，你可以看到两个exe文件，在amd64目录下面，你也能找到一个同名的exe文件。

![](http://dl2.iteye.com/upload/attachment/0116/5339/d28eca99-7d73-35ff-ab73-fd6bf4272bd4.png)  
 

![](http://dl2.iteye.com/upload/attachment/0116/5341/838f0e22-350a-32e3-beb2-44c2f163baf3.png)  
 

创建Windows Service，我们需要的是wildfly-service.exe这个文件，在%JBOSS\_HOME%\\bin\\service目录下的wildfly-service.exe是基于32位系统的可执行文件，同理amd64目录下则是针对64bit的exe。由于我是64位的系统，我就以64bit exe为例。

 

首先进入%JBOSS\_HOME%\\bin\\service\\amd64目录，把wildfly-service.exe文件拷贝到某个目录，改个名字，比如wildfly-service-standalone.exe，之后在把改过名字的文件剪切回%JBOSS\_HOME%\\bin\\service\\amd64目录。

![](http://dl2.iteye.com/upload/attachment/0116/5349/daa43c2f-bf5d-348b-84fa-862792a6da5d.png)  
 

之后，我们退回到%JBOSS\_HOME%\\bin\\service\\目录下，为service.bat这个文件做同样的操作。

![](http://dl2.iteye.com/upload/attachment/0116/5353/909c02dd-f137-3979-a76c-a603df51ace1.png)  
 

用文本编辑器编辑，我们新复制并改名的service-standalone.bat文件。修改第20、21、22行，为SHORTNAME, DISPLAYNAME和DESCRIPTION提供一个唯一的值，在这里我们简单的都为其加上-standalone的后缀。这里我们提供的Name和Description将来会被显示在我们创建的Windows service中，因此他必须是唯一的。

![](http://dl2.iteye.com/upload/attachment/0116/5357/7928f32d-c5ed-3ab8-9479-a3a2e5f7c861.png)  
 

接下来我们转到文件的第60行，把service所对应的exe文件改为我们刚刚复制并改名的那个。如果你是32位的系统，则需要修改第63行。

![](http://dl2.iteye.com/upload/attachment/0116/5359/c39f8d78-233c-3f3d-835d-4bdce10cba27.png)  
 

继续转到line 93 and line 94，由于我当前设置的standalone实例是WildFly的默认实例，因此实例所使用的端口都是标准端口，因此line 93不需要修改，如果你为你的instance设置了端口偏移量，比如100（-Djboss.socket.binding.port-offset=100），那么这里把localhost:9990改为localhost:10090。line 94, 因为我们是standalone模式下的instance，因此删掉master，把DC\_HOST这个配置项留空。

![](http://dl2.iteye.com/upload/attachment/0116/5361/544d01ab-b5cc-396e-b4b4-5ed74fa79bc3.png)  
 

之后我们来到line 177和line 179，在这里我们指定当前的这个实例启动文件、根目录以及端口偏移量。这里的配置和之前第一篇我们创建bat文件里所提供的配置基本相同。在这里我使用默认的standalone实例，但是为了演示，我依然添加了启动文件、根目录以及端口偏移量的配置项。line 177修改完成之后，修改line 179的日志路径，因为我这里使用的是默认实例，因此不做任何修改。

![](http://dl2.iteye.com/upload/attachment/0116/5371/1c8d74e7-9463-3667-8a1f-ce981cc8663c.png)  
 

至此我们完成了基本的配置工作，接下来就需要来install这个service了，打开控制台进入%JBOSS\_HOME%\\bin\\service目录键入service-standalone.bat install

   
![](http://dl2.iteye.com/upload/attachment/0116/5375/f3a6d3b7-7baf-35da-b4c2-705cc097fb74.png)  
 

提示成功之后，我们在Windows Service中就能找到我们刚创建的Service了

![](http://dl2.iteye.com/upload/attachment/0116/5377/72204799-76cb-3f5a-80e0-800415fbb1de.png)  
 

试着启动一下，回到standalone的log目录下，我们会发现多了几个文件。

![](http://dl2.iteye.com/upload/attachment/0116/5379/9442a29f-4d30-385f-9a59-f2a8ab197754.png)  
 

之后打开日志验证，已经成功启动。

```
2016-04-12 00:21:46,348 INFO  [org.jboss.as.connector.services.resourceadapters.ResourceAdapterActivatorService$ResourceAdapterActivator] (MSC service thread 1-7) IJ020002: Deployed: file://RaActivatorhornetq-ra
2016-04-12 00:21:46,350 INFO  [org.jboss.as.connector.deployment] (MSC service thread 1-6) WFLYJCA0002: Bound JCA ConnectionFactory [java:/JmsXA]
2016-04-12 00:21:46,350 INFO  [org.jboss.as.messaging] (MSC service thread 1-1) WFLYMSG0002: Bound messaging object to jndi name java:jboss/DefaultJMSConnectionFactory
2016-04-12 00:21:46,775 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0060: Http management interface listening on http://127.0.0.1:9990/management
2016-04-12 00:21:46,776 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0051: Admin console listening on http://127.0.0.1:9990
2016-04-12 00:21:46,777 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0025: WildFly Full 9.0.2.Final (WildFly Core 1.0.2.Final) started in 5049ms - Started 240 of 414 services (222 services are lazy, passive or on-demand)
```

 

同理，如果我们想uninstall这个service呢？很简单，首先停到你需要uninstall的service，之后打开控制台进入%JBOSS\_HOME%\\bin\\service目录键入service-standalone.bat uninstall

![](http://dl2.iteye.com/upload/attachment/0116/5381/e7a4e6ab-bb80-351e-93c3-d7889f15e687.png)  
 

控制台除了支持install和uninstall命令之外，还支持start, stop以及restart命令，在service-standalone.bat文件中的line 66到line 70也可以找到。

![](http://dl2.iteye.com/upload/attachment/0116/5383/f18bbd56-40e7-3259-8217-cc50a0b361e2.png)  
 

至此，我们便完成Windows Service的配置工作，你可以根据你的实际情况来install任意个你需要的Service。![酷](http://www.iteye.com/javascripts/tinymce/plugins/emotions/img/smiley-cool.gif "酷")