[SNMP4J] (WIP) SNMP4J CommonJ/Spring integration contrib, round 3

Brice Fines bfines at sermepa.es
Wed Mar 10 18:34:59 CET 2010


Hi Frank,

I made some little changes in SpringTaskExecutorWorkerTaskImpl, now I use 
the daemon parameter from SNMP4J.
I wrap the provided WorkerTask to use Spring SchedulingAwareRunnable, this 
way, I can submit daemon tasks and normal tasks to the executor.
Now, I do not get the WAS warnings about hung threads (WSVR0605W: Thread 
threadname has been active for hangtime and may be hung....).
This solution is better.
(I only use the name parameter for debugging purpose for now)
Regards



import org.snmp4j.log.LogAdapter;
import org.snmp4j.log.LogFactory;
import org.snmp4j.util.WorkerTask;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.SchedulingAwareRunnable;

/**
 * WorkerTask implementation based on Spring TaskExecutor.
 */
public class SpringTaskExecutorWorkerTaskImpl implements WorkerTask {

        /**
         * Logger.
         */
        final static LogAdapter LOGGER = LogFactory
                        .getLogger(SpringTaskExecutorWorkerTaskImpl.class
);

        /**
         * TaskExecutor.
         */
        private TaskExecutor taskExecutor;

        /**
         * Name.
         */
        private String name;

        /**
         * Worker task wrapper.
         */
        private WorkerTaskWrapper workerTaskWrapper;

        /**
         * Constructor.
         * 
         * @param taskExecutor
         *            a task executor.
         * @param name
         *            a name.
         * @param workerTask
         *            a worker task.
         * @param daemon
         *            true if daemon thread, false otherwise.
         */
        public SpringTaskExecutorWorkerTaskImpl(TaskExecutor taskExecutor,
                        String name, WorkerTask workerTask, boolean 
daemon) {
                this.taskExecutor = taskExecutor;
                this.name = name;
                this.workerTaskWrapper = new WorkerTaskWrapper(workerTask, 
daemon);
        }

        /*
         * (withoutJavadoc)
         * 
         * @see org.snmp4j.util.WorkerTask#interrupt()
         */
        public void interrupt() {
                LOGGER.debug("Thread interrupt: " + this.name);
                // do nothing
        }

        /*
         * (withoutJavadoc)
         * 
         * @see org.snmp4j.util.WorkerTask#join()
         */
        public void join() throws InterruptedException {
                LOGGER.debug("Thread join: " + this.name);
                // do nothing
        }

        /*
         * (withoutJavadoc)
         * 
         * @see org.snmp4j.util.WorkerTask#terminate()
         */
        public void terminate() {
                LOGGER.debug("Thread terminate: " + this.name);
                // do nothing
        }

        /*
         * (withoutJavadoc)
         * 
         * @see java.lang.Runnable#run()
         */
        public void run() {
                LOGGER.debug("Thread run: " + this.name);
                taskExecutor.execute(workerTaskWrapper);
        }

        /**
         * Wrapper for WorkerTask.
         *
         */
        private class WorkerTaskWrapper implements SchedulingAwareRunnable 
{

                /**
                 * Worker task.
                 */
                private WorkerTask workerTask;

                /**
                 * True if daemon thread, false otherwise.
                 */
                private boolean daemon;

                /**
                 * Constructor.
                 * 
                 * @param workerTask
                 *            a worker task.
                 * @param daemon
                 *            true if daemon thread, false otherwise.
                 */
                public WorkerTaskWrapper(WorkerTask workerTask, boolean 
daemon) {
                        this.workerTask = workerTask;
                        this.daemon = daemon;
                }

                /*
                 * (without Javadoc)
                 * 
                 * @see java.lang.Runnable#run()
                 */
                public void run() {
                        workerTask.run();
                }

                /*
                 * (without Javadoc)
                 * 
                 * @see 
org.springframework.scheduling.SchedulingAwareRunnable#isLongLived()
                 */
                public boolean isLongLived() {
                        return daemon;
                }

        }
}


More information about the SNMP4J mailing list