0

Problem:

How to run a jython having main function in Mule application?

Issue:

I have a simple Jython code invoked by mule flow.

Flow:

<mule xmlns:... 
   <flow name="wfileFlow1" doc:name="wfileFlow1">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP"/>
        <logger message="===\n START ===" level="INFO" doc:name="Logger"/>
        <scripting:component doc:name="Python">
            <scripting:script engine="jython" file="src/main/java/com/test/Test1.py"/>
        </scripting:component>
        <logger message="===\n END ===" level="INFO" doc:name="Logger"/>
    </flow>
</mule>

Test1.py

def add(a,b):
    return a+b

def addFixedValue(a):
  y = 5
  return y +a

print add(1,2)
print addFixedValue(1)

output:

===\n START ===
3
6

===\n END ===

if I run with main, then no output i.e it doesn't print anything.

Test1.py

def add(a,b):
    return a+b

def addFixedValue(a):
  y = 5
  return y +a

  if __name__ == '__main__':
      print add(3,4)
      print addFixedValue(1) 

It prints no jython values:

 ===\n START ===

 ===\n END ===

Note here no jython values are printed.

Issue is, since second one run as main program in Java, but how do I run a main program from mule application if my above flow is wrong?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
AKB
  • 5,918
  • 10
  • 53
  • 90

1 Answers1

0

__name__ variable is just a Python interpreter hack for executing sections in a main source file (and ignoring them in imported modules). Mule sets the value of __name__ to main when running Jython scripts, so I guess you could do:

if __name__ == '__main__' or __name__ == 'main':
Community
  • 1
  • 1
Anton Kupias
  • 3,945
  • 3
  • 16
  • 20