~ubuntu-branches/ubuntu/trusty/ehcache/trusty

« back to all changes in this revision

Viewing changes to src/main/java/net/sf/ehcache/constructs/nonstop/NonstopExecutorServiceImpl.java

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg
  • Date: 2013-05-06 14:53:07 UTC
  • mfrom: (1.1.7) (2.1.8 sid)
  • Revision ID: package-import@ubuntu.com-20130506145307-v5bhw5yu70re00l3
Tags: 2.6.7-1
* Team upload.
* New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/**
2
 
 *  Copyright 2003-2010 Terracotta, Inc.
 
2
 *  Copyright Terracotta, Inc.
3
3
 *
4
4
 *  Licensed under the Apache License, Version 2.0 (the "License");
5
5
 *  you may not use this file except in compliance with the License.
23
23
import java.util.concurrent.TimeUnit;
24
24
import java.util.concurrent.TimeoutException;
25
25
 
26
 
import org.slf4j.Logger;
27
 
import org.slf4j.LoggerFactory;
28
 
 
29
26
import net.sf.ehcache.CacheException;
30
27
import net.sf.ehcache.constructs.nonstop.concurrency.InvalidLockStateAfterRejoinException;
31
28
import net.sf.ehcache.terracotta.TerracottaNotRunningException;
32
29
 
 
30
import org.slf4j.Logger;
 
31
import org.slf4j.LoggerFactory;
 
32
 
33
33
/**
34
34
 * Class used by NonStopCache for executing tasks within a timeout limit.
35
35
 *
39
39
public class NonstopExecutorServiceImpl implements NonstopExecutorService {
40
40
 
41
41
    private static final Logger LOGGER = LoggerFactory.getLogger(NonstopExecutorServiceImpl.class);
 
42
    private static final String EOL = System.getProperty("line.separator");
42
43
    private final NonstopThreadPool nonstopThreadPool;
43
44
 
44
45
    /**
55
56
     */
56
57
    public <V> V execute(final Callable<V> callable, final long timeoutValueInMillis) throws TimeoutException, CacheException,
57
58
            InterruptedException {
 
59
        if (NonstopThread.isCurrentThreadNonstopThread()) {
 
60
            throw new AssertionError("Nonstop thread should execute inline instead of trying to use executor. Trying to execute callable: "
 
61
                    + callable + ", timeoutMillis: " + timeoutValueInMillis + ", current thread: " + Thread.currentThread().getName());
 
62
        }
58
63
        int attempt = 0;
59
64
        V result = null;
60
65
        long startTime = System.nanoTime();
61
66
        while (true) {
 
67
            boolean success = false;
62
68
            try {
63
69
                attempt++;
64
70
                result = nonstopThreadPool.submit(callable).get(timeoutValueInMillis, TimeUnit.MILLISECONDS);
 
71
                success = true;
65
72
                break;
66
73
            } catch (InterruptedException e) {
67
74
                // XXX: do something here?
102
109
            } catch (TimeoutException e) {
103
110
                // rethrow timeout exception
104
111
                throw e;
 
112
            } finally {
 
113
                if (!success) {
 
114
                    printNonstopThreadStackTrace(callable, timeoutValueInMillis);
 
115
                }
105
116
            }
106
117
        }
107
118
        return result;
108
119
    }
109
120
 
 
121
    private void printNonstopThreadStackTrace(Callable callable, long timeoutValueInMillis) {
 
122
        // check property at runtime always
 
123
        if (Boolean.getBoolean(PRINT_STACK_TRACE_ON_EXCEPTION_PROPERTY)) {
 
124
            StackTraceElement[] stackTrace = nonstopThreadPool.getNonstopThreadStackTrace();
 
125
            StringBuilder builder = new StringBuilder();
 
126
            builder.append("Nonstop thread Stacktrace for callable: ").append(callable).append(", timeoutValueMillis: ")
 
127
                    .append(timeoutValueInMillis).append(", current thread: ").append(Thread.currentThread().getName()).append(EOL);
 
128
            if (stackTrace.length > 0) {
 
129
                for (StackTraceElement ste : stackTrace) {
 
130
                    builder.append("  at ").append(ste.getClassName()).append(".").append(ste.getMethodName()).append("(")
 
131
                            .append(ste.getFileName() == null ? "<no file name info>" : ste.getFileName()).append(":")
 
132
                            .append((ste.getLineNumber() >= 0 ? ste.getLineNumber() + "" : "<unknown line number>") + ")").append(EOL);
 
133
                }
 
134
            } else {
 
135
                builder.append("<No stacktrace info available>");
 
136
            }
 
137
            LOGGER.info(builder.toString());
 
138
        }
 
139
    }
 
140
 
110
141
    private Throwable getRootCause(final Throwable exception) {
111
142
        Throwable e = exception;
112
143
        while (e.getCause() != null) {