This tutorial will show you how to use Mule Connector stdio in Mule based application.
What is Mule Connector (taken from Mule in Action)
A connector is in charge of controlling the usage of a particular protocol. It’s configured with parameters that are specific to this protocol and holds any state that can be shared with the underlying entities in charge of the actual communications. For example, a JMS connector is configured with a Connection, which is shared by the different entities in charge of the actual communication.
These communicating entities will differ depending on whether the connector is used for listening/polling, reading from, or writing to a particular destination: they would respectively be message receivers, message requesters, and message dispatchers. Though a connector isn’t part of a service, it contributes these communication parts to it. Consequently, a service is dependent on one or more connectors for actually receiving or sending messages.
In our example, we’d need an HTTP connector and a JMS connector. The HTTP connector would provide the message receiving infrastructure in the form of a dedicated listener on a particular port. The JMS connector would provide the capacity to connect to the target JMS provider (through its specific connection factory) and to handle the dispatch of messages to the desired queue.
Connectors provide an abstraction layer over data transport mechanisms. Connectors exist for things such as files, email messages, databases, JMS, and even Jabber messages. A connector saves you the tedium of having to implement the details of a particular communication mechanism yourself. This allows you to focus on solving your integration problem and not on the plumbing of a particular communications protocol.
Mule Studio 3.x(Anypoint Studio) (Download from https://www.mulesoft.com/platform/studio)
Maven 3.2.1 (Download from https://maven.apache.org/download.cgi?Preferred=ftp://mirror.reverse.net/pub/apache/)
JDK 1.7 (Download from http://www.oracle.com/technetwork/java/javase/downloads/index.html)
Configure JDK, Maven and Mule Studio
Step 1. First install JDK
Step 2. Add the Java_Home/bin directory to your system’s PATH.
Step 3. After downloading Maven, extract it to a drive
Step 4. Add the M2_Home/bin directory to your system’s PATH.
Step 5. Download and extract Mule Studio to a drive
Step 6. Now start Mule Studio by clicking on AnypointStudio exe icon in the folder <physical drive>/AnypointStudio
Step 7. Once started, close the startup page
Step 8. In Mule Studio, go to Window -> Preferences. Expand Java, then click on Installed JREs. Add JDK 1.7 and select it. In expanded Java, click on Compiler and select the compiler level as 1.7
Step 9. Now expand Anypoint Studio and click on Maven Settings. Then select appropriate Maven installation home directory using Browse button.
Step 10. If you want you can input Default groupId for new projects, it will save your time every time when you want to create a new project.
Create Mule project in Mule Studio
Now we will see how to create a new project in Mule Studio(Anypoint Studio).
Step 1. In Anypoint Studio, go to File -> New -> Mule Project
Step 2. Input Project Name: mule-3, Runtime is by default selected, tick on Use Maven; here the artifactId is automatically picked up from the Project Name:, the Group Id is picked up from the Default groupId for new projects and version is also a default value.
Step 3. Click Next and verify the JDK, mainly select Use default JRE(currently ‘jdk1.7.0_x’)
Step 4. Click on Next and click on Finish.
So when the project mule-3 is created in the Anypoint Studio, the project structure looks like below
Mule Connector – STDIO
This application uses the stdio transport to receive messages from the console input (stdin) and send these messages unchanged directly to the console output (stdout).
A specific component, called a bridge, is used to pass the messages from the inbound router to the outbound one. A bridge is a neutral component: it doesn’t perform any action or modify the messages that it processes. The outbound router is a passthrough one: it’s the simplest router that exists. It dispatches the messages it receives to a unique endpoint.
In this example, you will notice that there is no bridge component. This is because a service uses a bridge component implicitly if none is configured.
Components like pass-through-router can also have no behavior at all; in that case they are pass-through and make the service act as a bridge between its inbound and outbound routers. In essence, a component receives, processes, and returns messages. It is an object from which one method will be invoked when a message reaches it.
Step 1. You can rename the src/main/app/mule-3.xml file as stdio.xml file.
Step 2. Open the stdio.xml file and click on Configuration XML view in the Editor
Step 3. Modify the stdio.xml file as shown below
<?xml version="1.0" encoding="UTF-8"?> <mule xmlns:stdio="http://www.mulesoft.org/schema/mule/stdio" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.5.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.mulesoft.org/schema/mule/stdio http://www.mulesoft.org/schema/mule/stdio/current/mule-stdio.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd"> <stdio:connector name="StdioConnector" promptMessage="Please enter something : " messageDelayTime="1000" outputMessage="The response is : " /> <model name="stdioModel"> <service name="stdioService"> <inbound> <stdio:inbound-endpoint system="IN" /> </inbound> <outbound> <pass-through-router> <stdio:outbound-endpoint system="OUT" /> </pass-through-router> <pass-through-router> <stdio:outbound-endpoint system="ERR" /> </pass-through-router> </outbound> </service> </model> </mule>
In the above XML Configuration, we first define STDIO Connector with different behaviors such as promptMessage that indicates what would be shown in the Console for asking input from the user, outputMessage that indicates the response would be shown upon giving an input in the Console, messageDelayTime that indicates the delay between consecutive promptMessage.
Here we have inbound and outbound endpoints. An implicit bridge component is used to pass the messages from the inbound router to the outbound one. A bridge is a neutral component: it doesn’t perform any action or modify the messages that it processes. The outbound router is a passthrough one: it’s the simplest router that exists. It dispatches the messages it receives to a unique endpoint.
Running the application
Now do a right-click on the stdio.xml file or on the mule-3 project and click on Run As -> Mule Application. Then you will see something like below in Console when the application runs
********************************************************************** * Application: mule-3 * * OS encoding: Cp1252, Mule encoding: UTF-8 * * * * Agents Running: * * DevKit Extension Information * * Batch module default engine * * Clustering Agent * * JMX Agent * ********************************************************************** INFO 2016-06-20 07:23:15,394 [main] org.mule.module.launcher.MuleDeploymentService: ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + Started app 'mule-3' + ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ INFO 2016-06-20 07:23:15,399 [main] org.mule.module.launcher.DeploymentDirectoryWatcher: ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + Mule is up and kicking (every 5000ms) + ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Please enter something :
As prompted, enter something. Note that the first time a message gets dispatched to the outbound endpoint, Mule instantiates and connects the necessary dispatcher:
Please enter something : Hello INFO 2016-06-20 07:25:12,350 [[mule-3].stdioService.02] org.mule.lifecycle.AbstractLifecycleManager: Initialising: 'StdioConnector.dispatcher.25766141'. Object is: StdioMessageDispatcher INFO 2016-06-20 07:25:12,351 [[mule-3].stdioService.02] org.mule.lifecycle.AbstractLifecycleManager: Starting: 'StdioConnector.dispatcher.25766141'. Object is: StdioMessageDispatcher The response is : Hello Please enter something :
Next time onward Mule will not instantiates the dispatcher
Please enter something : Hi The response is : Hi Please enter something :
Thanks for reading.