Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spring @Scheduler Annotation Issues with jdk 1.8 & jdk 1.7 #157

Closed
Sudheer786 opened this issue Dec 28, 2015 · 10 comments
Closed

Spring @Scheduler Annotation Issues with jdk 1.8 & jdk 1.7 #157

Sudheer786 opened this issue Dec 28, 2015 · 10 comments

Comments

@Sudheer786
Copy link

Hi Team,

here is my piece of code,

@Configuration
@EnableScheduling
public class CSVFileProcessor {

@Scheduled(fixedRate = 10000)
    private static synchronized void pollDirectory() {
        System.out.println("***started Polling***");
        faxFolder = Paths.get(folderPath);

}

The above Code is running perfectly with Tomcat 7 & jdk 1.8

but when I use jdk 1.7 , The Server is getting hanged while trying to initialize
Spring FrameworkServlet 'appDispatcher' .

Dec 28, 2015 4:20:14 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'appDispatcher'

Is there anything to do with jdk Versions ?

can you help me please ?

@artembilan
Copy link
Member

Hello, @Sudheer786 !

  1. I'm not sure how the issue is related to the Spring Integration and its Samples.
  2. I'm not aware in any issue on the matter regarding Java versions.
  3. I see you use @Scheduled against private static method. Weird. How about to try the same for normal public method?

@Sudheer786
Copy link
Author

Hi Artem,

I made it as a static method as I need it running before the class gets Initialized,

here are the complete details,

  • The below Code is working fine on my local set up(windows , jdk 1.8,Tomcat 7)
  • the same Code is not working on the Development box (Linux,jdk 1.7,Tomcat 7)

Server Details:

Linux r007ppn0c 2.6.32-358.23.2.el6.x86_64 #1 SMP Sat Sep 14 05:32:37 EDT 2013 x86_64 x86_64 x86_64 GNU/Linux
jdk1.7.0_21/bin/java -Djava.util.logging.config.file=/xjp/appserver_1/tomcat/apache-tomcat-7.0.42/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Xms4096m -Xmx4096m -XX:PermSize=256m -XX:MaxPermSize=512m -Denv=Dev_CNJ -Dgrails.env=Dev_CNJ -Djava.awt.headless=true -Dliquibase.should.run=false -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseParNewGC -XX:NewSize=512m -XX:MaxNewSize=512m -XX:+UseConcMarkSweepGC -XX:+DisableExplicitGC -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -Xloggc:/xjp/tomcat/apache-tomcat-7.0.42/logs/gc.log -Dderby.system.home=/xjp/appserver_1/tomcat/apache-tomcat-7.0.42/derby -Dspring.profiles.active=Dev,ldap-properties -Ddomain=Dev -Ddivision=BNYMellon -DdataCenter=CNJ -Dcom.sun.management.jmxremote=ptql:State.Name.eq=java,Args.*.ew=catalina.base=/xjp/appserver_1/tomcat/apache-tomcat-7.0.42 -Djava.endorsed.dirs=/xjp/appserver_1/tomcat/apache-tomcat-7.0.42/endorsed -classpath /xjp/appserver_1/tomcat/apache-tomcat-7.0.42/bin/bootstrap.jar:/xjp/appserver_1/tomcat/apache-tomcat-7.0.42/bin/tomcat-juli.jar -Dcatalina.base=/xjp/appserver_1/tomcat/apache-tomcat-7.0.42 -Dcatalina.home=/xjp/appserver_1/tomcat/apache-tomcat-7.0.42 -Djava.io.tmpdir=/xjp/appserver_1/tomcat/apache-tomcat-7.0.42/temp org.apache.catalina.startup.Bootstrap start

Listener Code is as below:

@Configuration
@EnableScheduling
public class CSVFileProcessor {

       @Scheduled(fixedRate = 10000)
    public static synchronized void pollDirectory() {
        System.out.println("***started Polling***");
        faxFolder = Paths.get(folderPath);
        try {
            watchService = FileSystems.getDefault().newWatchService();

            faxFolder.register(watchService,
                    StandardWatchEventKinds.ENTRY_CREATE);
            do {
                WatchKey watchKey = null;
                try {
                    watchKey = watchService.take();
                } catch (InterruptedException e) {
                    System.out.println("Interrupted Exception cause "
                            + e.getMessage() + e);
                    e.printStackTrace();
                } catch (Exception e) {
                    System.out.println("Exception cause .. " + e.getMessage()
                            + e);
                }
                for (WatchEvent event : watchKey.pollEvents()) {
                    WatchEvent.Kind kind = event.kind();
                    if (StandardWatchEventKinds.ENTRY_CREATE.equals(event
                            .kind())) {
                        String fileName = event.context().toString();

                        System.out.println("File Created:" + fileName);
                        String txtFileAbsPath = faxFolder.toString()
                                .concat("\\").concat(fileName);
                        System.out.println("##### Text file absolute Path .. "
                                + txtFileAbsPath);

                        if (txtFileAbsPath.contains(".txt")) {
                            System.out
                                    .println("************** calling processFile() method on file : "
                                            + txtFileAbsPath);

                            processFile(txtFileAbsPath);
                        }
                    }
                }
                valid = watchKey.reset();

            } while (valid);
        } catch (IOException e) {
            System.out.println("IOException while polling Directory cause "
                    + e.getMessage() + e);
            e.printStackTrace();
        }

    }

}

and the error on Server as mentioned above ,

Dec 28, 2015 1:38:00 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'appDispatcher'

Is there anything you suspect on the above please ?

thanks In Advance .

@artembilan
Copy link
Member

I made it as a static method as I need it running before the class gets Initialized,

???

How you can make it running before class gets Initialized, if it is @Scheduled?
From other side it doesn't work until Spring Framework processes your class fully as bean.
See ScheduledAnnotationBeanPostProcessor.postProcessAfterInitialization():

Runnable runnable = new ScheduledMethodRunnable(bean, method);

So, we deal here with the bean exactly, not class. Therefore your static stuff does not make sense for Spring.

From other side it looks like the problem is somewhere in a different place and doesn't relate to the @Scheduled...
You should dump your Tomcat's process and see where is the stuck for your threads.

@Sudheer786
Copy link
Author

yeah , I got you .. sorry

below is the Exception where I am stuck ,

- locked <0x0000000700c244b8> (a java.lang.Class for com.activiti.extension.bean.CSVFileProcessor)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:601)

at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65)

at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)

at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)

at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:351)

at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:178)

at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:178)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:722)

"pool-3-thread-1" prio=10 tid=0x000000000119b800 nid=0x3ac4 waiting on condition [0x00007f9fd76c3000]
   java.lang.Thread.State: WAITING (parking)
    at sun.misc.Unsafe.park(Native Method)
    - parking to wait for  <0x00000006fccd0120> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
    at java.util.concurrent.locks.LockSupport.park(LockSupport.java:186)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2043)
    at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1085)

at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:807)

at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1068)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)

at java.lang.Thread.run(Thread.java:722)

"Thread-6" daemon prio=10 tid=0x0000000001e13000 nid=0x3ac3 in Object.wait() [0x00007f9fd77c4000]

java.lang.Thread.State: TIMED_WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x0000000702e167e0> 
(a java.lang.Object)
    at org.activiti.engine.impl.asyncexecutor.AcquireAsyncJobsDueRunnable.run(AcquireAsyncJobsDueRunnable.java:86)


- locked <0x0000000702e167e0> (a java.lang.Object)

- locked <0x00000007030c9258> (a org.activiti.engine.impl.asyncexecutor.AcquireAsyncJobsDueRunnable)

at java.lang.Thread.run(Thread.java:722)

"Thread-5" daemon prio=10 tid=0x0000000001e17800 nid=0x3ac2 in Object.wait() [0x00007f9fd78c5000]


java.lang.Thread.State: TIMED_WAITING (on object monitor)

at java.lang.Object.wait(Native Method)
    - waiting on <0x0000000702fba728>
 (a java.lang.Object)
    at org.activiti.engine.impl.asyncexecutor.AcquireTimerJobsRunnable.run(AcquireTimerJobsRunnable.java:88)

- locked <0x0000000702fba728> (a java.lang.Object)

- locked <0x00000007030c9280> (a org.activiti.engine.impl.asyncexecutor.AcquireTimerJobsRunnable)

this might be being caused as I kept synchronized for my @scheduled method.

 @Scheduled(fixedRate = 10000)
    public static synchronized void pollDirectory()

@artembilan
Copy link
Member

And? Does it help you somehow to overcome issue?
I see Activity is involved in the process.
So, more and more questions where is the problem with the Spring Integration?..

You have a dead lock with the fixedRate, when the next task is initiated after the start of the previous one. But yes, having synchronized there doesn't allow to go ahead and this new task is stuck. After the fixedRate = 10000 it initiates a new task again and so on.
Consider to use fixedDelay instead.

@artembilan
Copy link
Member

Note: please, read about the comments syntax here on the GitHub: https://guides.github.com/features/mastering-markdown/.

@Sudheer786
Copy link
Author

Hi Artem,
yeah , after removing 'static' and 'synchronized' specifiers from the method
The Server is up and running .

we are developing this to run along with Activiti , hence it becoming a little difficult to solve the Issues .

thanks a lot for your inputs which helped me to solve the Issue.

@artembilan
Copy link
Member

Glad to hear!

Let us know if we can close the issue or do that yourself.

Comeback to us anytime, but try to post the issue in the proper place.

@Sudheer786
Copy link
Author

Hi Artem,

thanks a lot .. sure , I will make sure to post in proper place.

I am closing the Issue.

@wshidaijingshen
Copy link

I'll give it a try then,thanks a lot ,gus

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants