11 Replies Latest reply on Feb 3, 2016 5:42 AM by ninjamester

    Start Windows service with different configuration

    James Wilson Newbie

      How do I set the server configuration for WildFly running as a Windows Service? The service batch file only seems to have options for installing in standalone or domain modes with the default configuration files. I want to have separate instances running for the different demos I have on a server, I can do it by setting the configuration when starting manually but can't find anything about doing this when starting as a service. Of course I can just duplicate the entire WildFly instance and modify standalone.xml for each, but I was hoping to avoid that.

        • 1. Re: Start Windows service with different configuration
          Rich DiCroce Novice

          I recently had this headache as well. As far as I can tell, the only easy way to change which configuration file is used is to edit bin/standalone.conf.bat (or domain.conf.bat) and add a line like this:

           

          set "SERVER_OPTS=%SERVER_OPTS% -c my-config-file.xml"

           

          If you're more adventurous, you might try mucking with bin/service/service.bat to make it so you can pass arbitrary parameters to standalone.bat/domain.bat, which would then allow you to create multiple services with different configurations.

          • 2. Re: Start Windows service with different configuration
            James Wilson Newbie

            Thanks for the reply Rich. From what I can tell service.bat sets a bunch of things then calls wildfly-service.exe with various arguments, and I can't find anything that tells me what I could add to that to change the configuration. Also looking at the service details in the registry there's nothing about parameters being passed. So it looks like for the moment I'm either going to have to replicate everything, or go back to using JavaService as we did for previous versions of JBoss AS.

            • 3. Re: Start Windows service with different configuration
              Rich DiCroce Novice

              The WildFly developers implemented the service stuff using Apache Commons Daemon, which is not very good in my experience. You're correct that it stores stuff in the registry, and I think that could be used to pass additional parameters, but not without some major surgery on the service.bat script. The key is the %STARTPARAM% variable. It looks like that ultimately just calls standalone.bat, and you can pass -c=my-config.xml to standalone.bat. So the trick is to be able to add arguments to STARTPARAM, which the current service.bat doesn't allow.

               

              Also, I think even replicating everything would not work unless you change the SHORTNAME at the top of the script. I believe that has to be unique or else each service installation will just overwrite the previous one.

               

              If you don't want to go that route, I'd recommend trying to wrap WildFly using YAJSW. I've used that on a couple projects and found that it works quite well. You may also want to open an enhancement request in JIRA and reference this thread.

              1 of 1 people found this helpful
              • 4. Re: Start Windows service with different configuration
                Tomaz Cerar Master

                Rich DiCroce wrote:

                 

                If you don't want to go that route, I'd recommend trying to wrap WildFly using YAJSW. I've used that on a couple projects and found that it works quite well. You may also want to open an enhancement request in JIRA and reference this thread.

                Care about sharing what you did, by sending PR for wildfly-core, or contribute wiki page with description how to use it?

                • 5. Re: Start Windows service with different configuration
                  Rich DiCroce Novice

                  I guess I should clarify that when I say I've used YAJSW, I mean that I've used it on non-WildFly projects. I have not used YAJSW with WildFly, so I don't have anything to contribute. I might look into it in the future though, as I've gotten a few complaints from various people on my current project about it being too hard to configure WildFly as a Windows service. If I do come up with something, I'll definitely open a JIRA and possibly send a PR. But that won't be for a while, as we've got an important demo coming up soon and I have bigger fish to fry.

                  • 6. Re: Start Windows service with different configuration
                    Tomaz Cerar Master

                    I was already getting excited that you used it with wildfly already and have working config Oh well...

                    If you do manage to look into it in the future it would be great.

                     

                    Also if there are any suggestions what can be done to improve current service stuff, let us know or even send PR

                    • 7. Re: Start Windows service with different configuration
                      James Wilson Newbie

                      Hurrah! It's possible. It's not simple, but I think the full instructions for each service (for each different instance replace 'myapp' by something suitable, and change the port offset) are:

                      Copy the standalone directory to one called myapp. Copy the standalone.conf.bat file in the bin directory to myapp.conf.bat. Add the following lines to myapp.conf.bat (just above :JAVA_OPTS_SET is a good place)

                      rem # Set base directory
                      set "JAVA_OPTS=%JAVA_OPTS% -Djboss.server.base.dir=myapp"

                      Copy the standalone.bat file in the bin directory to myapp.bat. Edit the line in myapp.bat from

                      set "STANDALONE_CONF=%DIRNAME%standalone.conf.bat"

                      to

                      set "STANDALONE_CONF=%DIRNAME%myapp.conf.bat"

                       

                      Offset the port numbers used so they're distinct - edit the port-offset in the socket-binding-group tag in myapp/configuration/standalone.xml. The value will be added to all ports used, so e.g. to make the web port 58080 use a value of 50000.

                      Copy service.bat (in bin/service) to service-myapp.bat. In this file change the name and description of the service, e.g.:
                      set SHORTNAME=Wildfly-myapp
                      set DISPLAYNAME="Wildfly-myapp"
                      set DESCRIPTION="Wildfly Application Server (myapp)"

                      Also edit the lines as follows:
                      Replace:
                      set STARTPARAM="/c \"set NOPAUSE=Y ^^^&^^^& standalone.bat\""
                      with
                      set STARTPARAM="/c \"set NOPAUSE=Y ^^^&^^^& myapp.bat\""
                      Replace:
                      set LOGPATH=%JBOSS_HOME%\standalone\log
                      with
                      set LOGPATH=%JBOSS_HOME%\myapp\log
                      Replace:
                      set CONTROLLER=localhost:9990
                      with
                      set CONTROLLER=localhost:<port_number>
                      where <port_number> is 9990 + whatever you offset the port number by in standalone.xml.

                      Now, in an administrator command prompt, go to bin/service and run service-myapp install.

                      • 8. Re: Start Windows service with different configuration
                        James Wilson Newbie

                        Tomaz, in terms of suggestions it would be nice if some of the above could be automated/scripted! I'm not sure what you mean by 'send PR', have you got a link you can give me so I can read up on it and hopefully do something helpful?

                        • 9. Re: Start Windows service with different configuration
                          Tomaz Cerar Master

                          James Wilson wrote:

                           

                          Tomaz, in terms of suggestions it would be nice if some of the above could be automated/scripted! I'm not sure what you mean by 'send PR', have you got a link you can give me so I can read up on it and hopefully do something helpful?

                          By send PR i meant, send pull request against wildfly-core repo that host this scripts https://github.com/wildfly/wildfly-core/tree/master/core-feature-pack/src/main/resources/content/bin

                           

                          Just for info, there is plan to move more or less all windows related scripts to powershell, as batch files just don't support as much functionality we need.

                          • 10. Re: Re: Start Windows service with different configuration
                            Rich DiCroce Novice

                            I spent some time tinkering with this and came up with a good solution using YAJSW.

                             

                            Rename or delete the bin/service directory in your WildFly install, then grab the zip file attached to this post and unzip it in bin. It will create a new service directory containing a slimmed down version of YAJSW (all optional libraries and other unnecessary stuff have been removed). The most important files are wrapper-wfly.conf and wrapper-wfly-app.conf. The former contains the base configuration that must always be present. Don't modify it! Instead, make your changes in wrapper-wfly-app.conf. If you need multiple service configurations, make copies of that file.

                             

                            In wrapper-wfly-app.conf (or the copy you made of it), there are a couple key things:

                            • wrapper.java.command specifies the Java executable to use to run the service. I've set the default to be whatever java is in PATH, but you can change it to an absolute path to a particular java.exe if necessary.
                            • The wrapper.java.additional properties specify additional configuration parameters for the JVM itself. I've pre-set it with the same parameters in standalone.bat and standalone.conf.bat.
                            • The wrapper.app.parameter properties specify additional arguments to be passed to WildFly. I've included a -c parameter to set the WildFly configuration file to standalone.xml. Change it if you want to use a different config file.
                            • wrapper.ntservice.name is the Windows service name and must be unique. Change it if you need to have multiple WildFly services installed. You don't have to change the displayname and description properties, but if you have multiple WildFly services installed, you might have trouble telling them apart. :-)

                             

                            You can test your configuration by opening a command prompt in bin/service and running "java -jar wrapper.jar -c <your conf file>". This will cause YAJSW to start WildFly using the wrapper configuration it will use when you install the service, but in a console so it's easier to debug any issues. Once you're satisfied with your configuration, run the same command again except with -i instead of -c to install the service. Use -r to remove the service. You can also run "java -jar wrapper.jar -?" for a full list of options. Note that service installation/removal must be done using an elevated command prompt. YAJSW will try to elevate automatically if necessary, but that did not work on my machine.

                             

                            I'm planning to integrate this solution into two of our projects, but I'd love to see it replace the current Commons Daemon wrapper in WildFly 9. The YAJSW solution has a lot of advantages:

                            • Doesn't involve daisy-chaining a bunch of batch scripts.
                            • Allows the same WildFly instance to be installed as multiple services. (Well, it should. I have not actually tested this.)
                            • No need to specify controller, username, or password parameters because it doesn't use the CLI to stop WildFly. Stopping the service is equivalent to pressing Ctrl+C in a console.
                            • Easy for developers building things on top of WildFly to provide custom service configurations. All they need to do is add their conf file to WildFly and make sure it includes the base wrapper-wfly.conf file.

                             

                            Of course, there are always some drawbacks:

                            • The YAJSW libraries are 11MB zipped. I know the WildFly devs have been trying to keep the WildFly download as small as possible, so this could be a problem. Many of the libraries YAJSW uses are already in the WildFly modules directory, but YAJSW does something special to find its libraries, so I'm not sure if it could be pointed to other locations.
                            • You need to disable or remove the console logger in the WildFly configuration. Leaving it in doesn't cause any errors, but YAJSW captures everything printed to System.out and System.err in its log, so you'll end up with double logging. That's the reason I put the log.console.level parameter in wrapper-wfly-app.conf, so I could use system property substitution to effectively disable the console logger in my WildFly configuration when running as a service.

                             

                            And there is also some unfinished work:

                            • The configuration I've provided works for standalone mode, but I have not tried to make it work for domain mode. We don't use domain mode, so that effort should probably be done by someone who understands domain mode better than I do.
                            • Having to run "java -jar wrapper.jar <parameters>" is a little unfriendly. This could be solved with a PowerShell script that mimics the service.bat currently included in WildFly.
                            • 11. Re: Start Windows service with different configuration
                              ninjamester Newbie

                              There are a comment in service.bat that addresses the issue with having multiple services:

                              REM If more then one service is needed, copy this file to another name

                              REM and use unique names for SHORTNAME, DISPLAYNAME, DESCRIPTION

                               

                               

                              But it's easy to support multiple services with 1 service.bat file:

                               

                              I made some small changes to service.bat to be able to choose the configuration-file.

                               

                              I just wanted to run 1 instance with custom configuration so i just added a parameter block like the one that handles /user  param

                               

                              if /I "%1"=="/configuration" (

                                if not "%2"=="" (

                                echo param = %2%

                                  set CONFIGURATION_XML=-c %2

                                ) else (

                                  echo ERROR: You need to specify a configuration

                                  goto endBatch

                                )

                                shift

                                shift

                                goto LoopArgs

                              )

                               

                              Then use this param in the line

                              set STARTPARAM="/c \"set NOPAUSE=Y ^^^&^^^& standalone.bat  %CONFIGURATION_XML%\""

                              So If CONFIGURATION_XML is not empty it will contain "-c xxx.xml"   where  xxx.xml is the name enteren with command "service install /configuration=xxx.xml"

                               

                               

                              The excact same can be done for  SHORTNAME  and  DISPLAYNAME which  enables multiple servers to be startet with this same .bat file