Python Jetlet
#
IntroductionIn this lesson, we will continue by creating another Jetlet. A Jetlet is a component that you can create yourself to successfully accomplish a goal and allow you to create your own reusable component. Jetic offers several components that perform a myriad of functions but every now and then you will find yourself wanting to separate certain logic and create something to reuse in your projects.
#
VisionsCorp’s requestVisionsCorp (VC) needs to perform a Python function after string data is received. VisionsCorp decided that it would best use a Jetlet, after having used the one you made in a previous lesson. The Jetlet they need today is one that will perform a Python function to mutate any string and capitalize it.
- Log
- Convert Body
- Set Headers
- Bean
- Remove Headers
#
Step 1: Create a new Jetlet- In the Management console, click on Jetlets in the side bar
- Click on ‘Add Jetlet’
- Name the Jetlet ‘PythonSink’
#
Step 2: Adding the components- Drag a Log component into the swing-lane
- In the Message tab add Executing python script. This way you will know when your script is being run.
- Under the Log, bring in a Convert Body and in the Property : TARGET CLASS NAME add: java.lang.String
- Mark that it is mandatory
#
Step 3: Manipulate headersAdd a Set Headers component under the Convert Body component
For the Headers we will put script use Simple as the language and for the value place this resource:file:/etc/camel/resources/{{script}}
This only works if there are certain resources that you have added in the Resources tab. We have added upper.py, lower.py, and script.py files with our necessary configurations. Since this is a demo environment you have access to the resources are already there. If you are reading this tutorial then and are in your own org you may choose to provide your own resources and add a variation to this integration
Add another property named function add the Constant declaration and then add {{function}}
Click on the Apply ▶️
#
Step 4: Add a bean- Add a Bean component to your route under the Set Headers
- Add PythonBean as the BEAN NAME
- To add the bean, go to resources -> Beans -> Create new bean
- Name the bean PythonBean , add a type of Java
- For the imports add:
jsx import org.apache.camel.BindToRegistry; import org.python.core.Py; import org.apache.camel.Body; import org.apache.camel.Header;
- Java Beans:
#
Step 5: Remove Headers- Drag in two Remove Headers components
- Add the PATTERN script to the first one and function to the following
- In the EXCLUDE PATTERN put Camel* in both
🎉 Congratulations you have successfully created your new Jetlet. With this Jetlet you can manipulate string data with a Python function. As you grow more comfortable using Jetlets you will see how you can further encapsulate your integrations as your use cases grow. 🎉