Revision: 15561
Author: baranowb
Date: Tue Nov 30 00:45:01 2010
Log: Fixes Issue 2147
Fix NPE due to cut throat condition.
http://code.google.com/p/mobicents/source/detail?r=15561
Modified:
/trunk/protocols/ss7/mtp/mtp-impl/src/main/java/org/mobicents/protocols/ss7/mtp/MTPScheduler.java
/trunk/protocols/ss7/mtp/mtp-impl/src/main/java/org/mobicents/protocols/ss7/mtp/MTPTask.java
=======================================
---
/trunk/protocols/ss7/mtp/mtp-impl/src/main/java/org/mobicents/protocols/ss7/mtp/MTPScheduler.java
Thu Jul 29 09:13:46 2010
+++
/trunk/protocols/ss7/mtp/mtp-impl/src/main/java/org/mobicents/protocols/ss7/mtp/MTPScheduler.java
Tue Nov 30 00:45:01 2010
@@ -27,11 +27,14 @@
package org.mobicents.protocols.ss7.mtp;
+import org.apache.log4j.Logger;
+
/**
*
* @author kulikov
*/
public class MTPScheduler {
+ protected static final Logger logger =
Logger.getLogger(MTPScheduler.class);
protected MTPTask[] tasks = new MTPTask[5];
MTPTask tempTask = null;
@@ -39,7 +42,7 @@
public void schedule(MTPTask task, int delay) {
task.deadLine= System.currentTimeMillis() + delay;
for (int i = 0; i < tasks.length; i++) {
- if (tasks[i] == null) {
+ if (tasks[i] == null || tasks[i].canceled) {
task.canceled = false;
tasks[i] = task;
task.index = i;
@@ -53,12 +56,28 @@
long now = System.currentTimeMillis();
for (int i = 0; i < tasks.length; i++) {
if (tasks[i] != null && (tasks[i].deadLine <= now)) {
- tempTask = tasks[i];
- tempTask.run();
- //tempTask = null;
+ //check if has been canceled from different thread.
if (tasks[i].canceled) {
tasks[i] = null;
- }
+ }else
+ {
+ tempTask = tasks[i];
+ try{
+ tempTask.run();
+ }catch(Exception e)
+ {
+ if(logger.isDebugEnabled())
+ {
+ logger.debug("Failuer on task run.",e);
+ }
+ }
+ //check if its canceled after run;
+ if (tasks[i].canceled) {
+ tasks[i] = null;
+ }
+ }
+ //tempTask = null;
+
}
}
}
=======================================
---
/trunk/protocols/ss7/mtp/mtp-impl/src/main/java/org/mobicents/protocols/ss7/mtp/MTPTask.java
Thu Jul 29 09:13:46 2010
+++
/trunk/protocols/ss7/mtp/mtp-impl/src/main/java/org/mobicents/protocols/ss7/mtp/MTPTask.java
Tue Nov 30 00:45:01 2010
@@ -26,6 +26,7 @@
*/
package org.mobicents.protocols.ss7.mtp;
+
/**
*
* @author kulikov
@@ -36,12 +37,17 @@
protected long deadLine;
protected int index;
protected MTPScheduler scheduler;
-
public void run() {
if (!canceled) {
- perform();
- }
- canceled = true;
+ try{
+ //exception in caught in scheduler.
+ perform();
+ }finally
+ {
+ canceled = true;
+ }
+ }
+
}
public boolean isCanceled() {
@@ -52,9 +58,10 @@
public void cancel() {
this.canceled = true;
+ //dont do this, let it be lazely reclaimed if ever, this causes
race!
//remove task from list
- if (scheduler != null && (index >=0) && (index <
scheduler.tasks.length)) {
- scheduler.tasks[index] = null;
- }
+ //if (scheduler != null && (index >=0) && (index <
scheduler.tasks.length)) {
+ // scheduler.tasks[index] = null;
+ //}
}
}