1414import org .apache .commons .lang3 .StringUtils ;
1515import org .apache .logging .log4j .LogManager ;
1616import org .apache .logging .log4j .Logger ;
17+ import org .apache .logging .log4j .ThreadContext ;
1718import org .mozilla .javascript .Context ;
1819import org .mozilla .javascript .Script ;
1920import org .mozilla .javascript .Scriptable ;
@@ -28,6 +29,8 @@ public abstract class JavaScriptTask<T> implements Callable<T> {
2829 private Logger logger = LogManager .getLogger (JavaScriptTask .class );
2930 private MirthContextFactory contextFactory ;
3031 private String threadName ;
32+ private String channelId ;
33+ private String channelName ;
3134 private Context context ;
3235 private boolean contextCreated = false ;
3336
@@ -74,6 +77,9 @@ private JavaScriptTask(MirthContextFactory contextFactory) {
7477 }
7578
7679 private void init (String name , String channelId , String channelName , Integer metaDataId , String destinationName ) {
80+ this .channelId = channelId ;
81+ this .channelName = channelName ;
82+
7783 StringBuilder builder = new StringBuilder (name ).append (" JavaScript Task" );
7884 if (StringUtils .isNotEmpty (channelName )) {
7985 builder .append (" on " ).append (channelName );
@@ -111,8 +117,22 @@ public final T call() throws Exception {
111117 String originalThreadName = Thread .currentThread ().getName ();
112118 try {
113119 Thread .currentThread ().setName (threadName + " < " + originalThreadName );
120+
121+ if (channelId != null ) {
122+ ThreadContext .put ("channelId" , channelId );
123+ }
124+ if (channelName != null ) {
125+ ThreadContext .put ("channelName" , channelName );
126+ }
127+
114128 return doCall ();
115129 } finally {
130+ if (channelName != null ) {
131+ ThreadContext .remove ("channelName" );
132+ }
133+ if (channelId != null ) {
134+ ThreadContext .remove ("channelId" );
135+ }
116136 Thread .currentThread ().setName (originalThreadName );
117137 }
118138 }
0 commit comments