Yunus Einsteinium
Yunus Einsteinium

Reputation: 1180

Create Windows Service using java

I developed a desktop app(client) that collects some data. It works even in the absence of internet connection.

I would like to create a windows service probably in java also, that everytime an internet connection is established, takes data from the apps database and sends it to some remote server.

I have been searching the internet for so long on how and what to use to create a windows service in java, and what i got is

Java Service Wrapper

People around the internet seem to use it, but for me i havent been able to find a proper docs to illustrate on how i can create a service.

Could anyone help with anything that will enable me to create a windows service or anything like that.

Upvotes: 1

Views: 343

Answers (1)

user2993722
user2993722

Reputation: 26

Add something like to your pom.xml:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>appassembler-maven-plugin</artifactId>
  <version>1.7</version>
  <executions>
    <execution>
      <id>generate-jsw-scripts</id>
      <phase>package</phase>
      <goals>
        <goal>generate-daemons</goal>
      </goals>
      <configuration>
        <repositoryLayout>flat</repositoryLayout>
        <includeConfigurationDirectoryInClasspath>true</includeConfigurationDirectoryInClasspath>
        <daemons>
          <daemon>
            <id>${jsw_name}</id>
            <mainClass>${path_to_runner_class}</mainClass>
            <commandLineArguments>
              <commandLineArgument>start</commandLineArgument>
            </commandLineArguments>
            <jvmSettings>
              <initialMemorySize>512M</initialMemorySize>
              <maxMemorySize>512M</maxMemorySize>
              <!--systemProperties><systemProperty>com.sun.management.jmxremote</systemProperty><systemProperty>com.sun.management.jmxremote.port=9010</systemProperty><systemProperty>com.sun.management.jmxremote.local.only=false</systemProperty><systemProperty>com.sun.management.jmxremote.authenticate=false</systemProperty><systemProperty>com.sun.management.jmxremote.ssl=false</systemProperty><systemProperty>org.tanukisoftware.wrapper.WrapperManager.mbean=TRUE</systemProperty><systemProperty>org.tanukisoftware.wrapper.WrapperManager.mbean.testing=false</systemProperty></systemProperties-->
            </jvmSettings>
            <generatorConfigurations>
              <generatorConfiguration>
                <generator>jsw</generator>
                <includes>
                  <include>linux-x86-64</include>
                  <include>windows-x86-64</include>
                </includes>
              </generatorConfiguration>
            </generatorConfigurations>
            <platforms>
              <platform>jsw</platform>
            </platforms>
          </daemon>
        </daemons>
      </configuration>
    </execution>
  </executions>
</plugin>

put name instead of jsw_name e.g. jsw_example and path to you runner class(class with main function) e.g. com.copper.drivers.emulation.Runner instead of path_to_runner_class. Then build the project. Go to the target\generated-resources\appassembler\jsw\${project_name}\bin\ and there you will found files to run from windows and from linux. Open cmd as admin and run them using command: ${project_name} console - if you whant to run it in console ${project_name} start - to run it as service.

If you need jmx uncomment systemProperties.

P.S. If you use linux - start it from ${project_name} directory with command bin/${project_name} start or bin/${project_name} console.

Upvotes: 1

Related Questions