I should have looked at the code first.  You could

A) make running a member variable and set it to false before
calling interrupt(), or 
B) use  "while (!interrupted())" instead of "while (running)"

If you do A) you should wrap the set and check inside
a synchronized block.

One problem with B) is that calling interrupted() resets
the flag, so the second time you call it, it will return
false.  This shouldn't be an issue in your case since you
aren't calling any routines would be doing any thread stuff.

JD

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
Sent: Thursday, September 25, 2003 1:09 AM
To: jboss mailing list 
Subject: [JBoss-user] mbean question


Hi all, 

I'm trying to write a basic MBean. 
This Mean runs a Thread which checks the file system for new files. If a 
new file is found it should perform some action. 

My problem however is that whenever I undeploy, or stop the MBean using 
the jmx-console application, the MBean keeps running. 

I have no idea what I'm doing wrong here.

Below is my code. Could somebody please tell me what it is that I'm doing 
wrong here?

Thanks a lot,

Harm de Laat
Informatiefabriek
The Netherlands


package nl.informatiefabriek.jmxlizard;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Vector;

import org.jboss.system.ServiceMBeanSupport;

/**
 * @author harm
 */
public class JBossLizard
        extends ServiceMBeanSupport
        implements JBossLizardMBean, Runnable {

        private Thread lizard;

        public JBossLizard() {
        }

        public String getName() {
                return "JBossJMXLizard";
        }

        /* (non-Javadoc)
         * @see java.lang.Runnable#run()
         */
        public void run() {

                boolean running = true;
                while (running) {
                        /* wait for interval milliseconds */
                        sleep(interval);

                        /* do some checking */
                        File checkDir = new File(absoluteCheckPath);
                        if (!checkDir.exists()) {
                                log.warn(
                                        "Directory does not exist: " + 
checkDir.getAbsolutePath());
                                sleep(1000 * 5);
                                // we wait for five seconds to avoid a 
huge system load
                                continue;
                        }

                        if (!checkDir.isDirectory()) {
                                log.warn(
                                        "Directory: "
                                                + 
checkDir.getAbsolutePath()
                                                + "is not a directory.");
                                sleep(1000 * 5);
                                continue;
                        }

                        /* retrieve the listing */
                        String[] dirlist1 = checkDir.list();
                        for (int i = 0; i < dirlist1.length; i++) {
                                /* don't touch hidden files */
                                if (dirlist1[i].startsWith(".")) {
                                        // System.out.println("Hidden 
file... Leaving it untouched: " + dirlist1[i]);
                                        break;
                                }

                                /* get file extension */
                                int index = dirlist1[i].lastIndexOf('.');
                                String checkFileExtension =
                                        dirlist1[i]
                                                .substring(index + 1, 
dirlist1[i].length())
                                                .toLowerCase();
                                System.out.println(
                                        "File extension is: " + 
checkFileExtension);
                                /* see if it is a file we should process 
*/
                                if (checkFileExtension
                                        .equals(this
.getFileExtension().toLowerCase())) {
                                        System.out.println(
                                                "This is the correct file 
extension.");
                                        /* Create a file handler */
                                        File f = new File(checkDir, 
dirlist1[i]);
 
                                        // process the file here!
                                }
 
                        }
                }
                System.out.println("Lizard stopped...");
        }

        private void sleep(long howlong) {
                try {
                        Thread.sleep(howlong);
                } catch (Exception e) {
                }
        }

        /* MBean methods */
        protected void startService() throws Exception {
                // Create a new thread with this monitor
                lizard = new Thread(this, "JBossMonitor");
                // Set it as a daemon
                lizard.setDaemon(true);
                // start the thread
                lizard.start();
        }

        protected void stopService() {
                lizard.interrupt();
        }
}


-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
JBoss-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-user


-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
JBoss-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-user

Reply via email to