If you would like to upgrade to a newer long-term support version of Studio Pro, see Moving from Mendix Studio Pro 8 to 9.
Java Programming
Introduction
With Java actions, you can extend the functionality of your application in situations where it would be hard to implement this functionality in microflows.
For a deep-dive look into Java programming in Mendix, check out this video:
For information about Java actions in Studio Pro, see Java Actions.
Writing Code in the .java Files of Your Java Actions
In the java files of your Java actions, you can write your Java code between the markers:
// BEGIN USER CODE
and// END USER CODE
// BEGIN EXTRA CODE
and// END EXTRA CODE
This is explained in more detail below.
The other code in these files is regenerated each time when you deploy your model, thus any modifications that you make in them will be overwritten. However, your imports will be preserved.
A Java action generated by Mendix Studio Pro. This Java action has no input parameters and simply returns a Boolean with value true
.
The method executeAction is called by the Runtime when the Java action is being executed. Between the lines // BEGIN USER CODE
and // END USER CODE
, you can write your custom code that will always be called when executing the action. In this method, you can call other methods in the section between // BEGIN EXTRA CODE
and// END EXTRA CODE
.
The executeAction method throws all exceptions that occur. This means you can do error handling in the microflow calling this Java action. If you would like to do your own error handling within the action, use try/catch statements.
Using the Mendix Java library
In the Java code that you write for your Java actions, you can use the Mendix Java library.
This library is automatically added to your libraries when you imported your project into Eclipse, and it is called mxruntime.jar.
For details on usage and example, see How to Use the Java API.
Opening HTTP Connections
Most cloud infrastructure services (including those used by Mendix Cloud) will close HTTP connections automatically if there is no traffic for a few minutes, even if your activity is still waiting for a response. This means that, if your activity calls a web service which takes a long time to respond, the connection may be closed without the activity being aware of this and your activity will not receive a response, but instead get stuck waiting indefinitely for data to arrive.
You should therefore ensure that you always set a timeout for any connections you make in your custom Java code.
Using Eclipse as an Environment to Write Your Mendix Java Actions
For details on this topic, see Using Eclipse.