Welcome back. In this video, we are going to understand the modules in mule four. This is a big change from New three to node four. We'll understand about what are modules in mule four. And we'll start using one module colors Java module. Okay, before I start working on this and describing this what I want is I hope you have seen my earlier section on lab documentation.
And you have downloaded the lab documents. zip file in the downloaded zip file. If you see there is one other zip file called us, mule, four Ws. Maybe were time you see this video. Maybe I might have changed this name. Let's assume that it is we will for Ws So I've extracted this workspace this workspace, which I have created already and I have extracted this here.
This contains a lot of projects which are pre built. I want to use this as my workspace in my any points to do. I'll copy this path. Then I'll go to File, switch workspace. Other and paste this launch. The studio will restart with a new workspace.
It is fate. Yes, it's truly opened with the new workspace that we show you Okay. So here we can see there are a lot of products which I have created for you. Actually for each and every topic, there will be one solution project. And one start project you can see here it is a pattern which I followed solution start solutions that solution will contain the whole solution and start project will contain some of the files, which are required for you to get started working on. So, right now, I'll be working on a basic start project.
I'll double click on it, it was if I double click I, okay. So it got open. And when I'm working on any of the project, I want all the other projects to be closed. So right. And I'll say close, unrelated projects. Are you sure?
Yes. Okay. So all the other projects could have been closed. I'll minimize this. I don't want this outline all okay. Now here I have already created a file colors using Java module XML and work on it.
Of course, this is an empty XML. But in this project, I have some Java code. There's a class called us talk code service. Let us see how many methods are there. There are two methods. One method is a static method say hello, which is taking two arguments and another is non static method.
So now in this project, I want to use a Java module and invoke this static method non static method. So, let's go actually If you see in my POM dot XML initially just open my POM dot XML. In this pomerode XML, I will go to the dependencies section. If you see there are two dependencies only HTTP connector dependency and socket. Actually these are the modules by default. If I go back to my XML again, in graphical mode, you can see that in the mule palette on the left side you can see HTTP and sockets are added.
There are no other modules. If I want to use Java module add, click on Add modules and select the module Java and drag and drop it to the left side. So now the Java module is added. Let me open this formula XML again. Yep, see, has been updated. I'll see yes.
So if I go to dependencies section right now, you can see that in the dependencies, there is one more dependency added right now new Java module. That means in mule for whatever modules I'm using, I can just configure them as dependencies in my POM and what our dependencies are added in the palm, they will be automatically added the build path you can see Java here and that module is available. And once I select this module, whatever components that are part of module will be available on the right side. Okay, now you understood how the module system works. Now, I want to drag and drop one HTTP flow, and drag and drop HTTP listener. And as usual, I'll configure one connector configuration One ad and I take default aids right one and base path I will not do, okay.
I'll do here path as slash Hello, simply. Now what I want is whenever a request comes, I want to invoke this static method. Say hello, which is taking two arguments. So I'm expecting name and city to come with query parameters. So when somebody is giving a request to slash Hallo, I'll expect them to send slash Hello question mark. Name is equals to say Siva and CT is equals to save bandwidth.
So I'm expecting a request to come in the request data Name and city using this name and city. I want the static method Say hello to be invoked. Right. So what to do? What I can do is simply I'll go to our module drag and drop invoke static so I'll maximize this as of now which class already there is a Java class if there is no Java class and if you want to create a Java class I can click on Add button I will click on such What is the name of that? stock caught service stock cut service okay.
And which method that method name is say Hello Is it just copy paste say hello which is taking string comma string so, what go here and configure say hello, which is taking string, comma string, that's fine. But I told him what static method to call, how to pass the value of arguments. Here you can see arcs as it. Now, here there is an icon show graphical view of the arcs. I can select this. Once I select, here we can write an expression to set the arcs.
Right. But actually, the methods take two arguments I expect on the right side to contain two argument which is not as expected. Sometimes studio will be here like this. I just click on Close I'll go back to one more file and come back. Okay, now let me click on this. See now, on the right side you can see say hello, which is converting RS zero and r one, right?
I'm expecting this og zero or one as part of query parameters. On the left side you can see attributes and in the attributes is a query params. Yes. So I want to map this query params to Rs zero and r1. What all I have to do is I just have to drag and drop. Okay, so drag and drop, again, drag and drop.
So basically, you are generating data, the expression, the Data View version is two dot zero. Right? Don't worry about what is this data wave as of now we'll have a detailed list section of data Data View, where we'll write starting from basic data expression to complex data expressions. But as of now, for now, what I want you to understand is, in Data View, curly braces, if you write something in curly braces, that means it is a map. So basically in JSON also, you might be knowing if you're working with Jason. in JSON, JavaScript Object Notation, whatever you write inside curly braces is a map or an object.
And whatever we write inside square bracket is an array or a list is it. This data also follows JSON like syntax. So here is a map configured, keys are zero, not one. Whatever I give as a value for zero will go as first argument. I'm expecting query parameter columns. So read dotnet.
And here I'll expect a query parameter called us city. So here I'll say dot city. So what I'm doing I'm mapping query parameters name and city to zero and one. Okay? That's all I told what are the arguments? Right?
So if I maximize, hear arguments Don't worry this is not shown properly. You can see this inside the XML configuration. In the XML configuration, you can see that the tag name is invoke static where the arcs actual the value of arcs is a data v expression, which is a map is it. So that's it. Now, let me run and check. If I run what I should get.
I should get Hello plus name plus from city plus it from city Okay, so now let me deep run it okay deployed. So how do we request right now? What is Melissa configured with slash Hello sir to give a request to slash Hello name and city okay let me do a request procedure console is the exception no Yep, I got Hello the name concatenated with from Bangor right? Yeah. So static method was invoked very nice. So now you understood how to invoke a static method.
Now, let me show you the next one how to invoke a non static method. This is a little bit tricky. So what I will do is again, I will drag and drop one more flow. We teach it is not a good spot as slash code. So, actually this method if you see the non static method, it is taking a stock symbol and returning the price just a hardcoded in the stock symbol is military 100 IBM it is returning 200 l stock doesn't traitor. So, I want to invoke this non static method passing is proxy symbol and I'll be getting a stock price.
So, I wrote class coats. Now, if I go to Java module, there is an operation called as new. I will drag and drop this one. These actually create an object This operation is used to create an object and store it in a variable Which class is that? The class has stock code. And what is the constructor?
Right now I want to invoke default constructor. So what I'll do is I'll give the class name without any arguments. Suppose if I have a constructor with arguments, argue, let's say I have an R, I have a constructor to take string comma string as argument. How to give it like is another pass arguments here, as I should have shown you earlier. But right now, I don't want to pass any arguments. I want to invoke a default constructor.
That's fine. Now, this new operator of Java module will create an object of this class using in using this constructor specified But I want it stored in a variable how to store it in a variable. I'll click on Advanced tab. And here I'll give a variable name. Stock code services q s, these are variable name I want to create and work with attributes value, the payload, the output of this component is what? The new object which is created using this constructor.
That's now the next top next thing is here, I'll drag and drop invoke. After new I will drag and drop invoke here and which instance I will write an expression. I'll rate as well as dot. Yeah, there's a variable called Sq s. So on this object, this is an instance where as our disk is pointing to an object on this What is a method to be invoked? The method name is get stock price it is taking string. So here I'll select z invoke component.
Class I'll give the class name as again the stock code surveys. And the method name is this one which is taking string. Okay, there's only one argument out of pass a valid argument if I click on this it is not showing the method name and argument on the right same as the content. So you know what to do. Just have to go to the another file and come back to reinitialize. Then if I maximize this invoke, and if we click on this graphical view Yep, now you can see get stock price argument.
I'm expecting the argument from let's say query parameter. I'll drag and drop it. So query parameter name. I'm expecting as dot stock. Done. So I configured to invoke this method on which instance vas.sq s. That's it.
The return value of the method will be the payload. I will drag and drop a logger and just log your cash payload. Okay. Now, let me debug. I want to debug and keep a breakpoint. I will kill the existing product.
Which is running application and then I will debug Okay, it's asking me to switch to debug perspective. Yes. No let me do a request to localhost 8081 slash caught. Question mark stock is it goes to IBM, I gave a request. Now it is here. When it is at this point.
Still the new is not executed. If you see in the vast the map size zero, I will press f6 it came it will come to the next component. You can see in the verse, there is SQL it is an object of Stop car service. Now if I say if I say f6 it will invoke the method. And now if you see the payload or the payload 200 because it paused IBM the method is returning 200 The 200 will be the mule message payload which command responsible press f8 now, I should see that 200 response by 200 because I pass the stock value of stock symbol as IBM and in this method if the value was this IBM it is running 200 So, now you understood how to invoke a non static Java method. Great.
What you can do is after this video, you have to follow a documentation in the lab documentation which you have downloaded from section two can just open this using Java module. Here are detailed instructions on what you have to do. Follow this document and complete the exercise. See you next video.