commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sebb <seb...@gmail.com>
Subject [DAEMON] Re: prunsrv crashes when exiting
Date Tue, 16 Oct 2012 09:15:23 GMT
On 16 October 2012 07:02, Majid Azimi <azimi.majid@yahoo.com> wrote:
> Hi guys,

Commons user list is shared, so the subject line must be prefixed with
the component name, as I have done in this reply.

> I have written a simple windows service. It only creates a thread and that thread logs
a random UUID to a file every 1 second.
>
> public class WindowsService {
>     private static MyRunnable r;
>     private static Thread t;
>
>     public static void main(String[] args) {
>         r = new MyRunnable();
>         t = new Thread(r);
>         t.start();
>     }
>
>     public static void start(String[] args) {
>         main(args);
>     }
>
>     public static void stop(String[] args) {
>         r.setStopped(true);
>         try {
>             t.join();
>         } catch (InterruptedException ex) { /* do nothing */ }
>     }
> }
>
>
> public class MyRunnable implements Runnable {
>     private boolean isStopped;

isStopped needs to be volatile.

>     private FileHandler handler;
>
>     public MyRunnable() {
>         try {
>             handler = new FileHandler("C:\\access.log", 100000, 5, true);
>         } catch (IOException | SecurityException ex) { /* do nothing */}
>
>         isStopped = false;
>     }
>
>     @Override
>     public void run() {
>         handler.publish(new LogRecord(Level.INFO, "starting service"));
>
>         while (!isStopped) {

Not guaranteed to pick up changes to the isStopped variable.

>             handler.publish(new LogRecord(Level.INFO, UUID.randomUUID().toString()));
>             try {
>                 Thread.sleep(1000L);
>             } catch (InterruptedException ex) {
>                 isStopped = true;
>             }
>         }
>
>         handler.publish(new LogRecord(Level.INFO, "exiting service"));
>     }
>
>     public synchronized void setStopped(boolean isStopped) {

Synchronization is useless unless both the writer and reader
synchronise on the same lock.
Drop the sync. qualifier and use volatile.

>         this.isStopped = isStopped;
>     }
> }
>
> I add this service with this command:
>
>
> prunsrv.exe //IS//MyService --DisplayName "Log Service" --Jvm auto --StartM
> ode jvm --StopMode jvm --Classpath "My jar file path" --StartClass win
> dowsservice.WindowsService --StopClass windowsservice.WindowsService --StartMeth
> od start --StopMethod stop
>
>
> I can start and stop the service and I can see "starting service" and "exiting service"
at the log file. But when I stop the service I got this error:
>
> Commons Daemon Service Runner has encountered a problem and needs to close. We are sorry
for the inconvenience.
>
> in the Event Viewer i see this message:
>
> Faulting application prunsrv.exe, version 1.0.10.0, faulting module jvm.dll, version
23.3.0.1, fault address 0x0012baba.
>
> Why is this happening? What is wrong with this program?

One obvious fault (as above) but I'm not sure that can cause the crash.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message