Task 2-1: Creating a Java agent
The first agent that you create reacts to transaction events by calling an external service.
About this task
Procedure
- In the Solution Map view, click Add Java agent.
- In the New Java Agent Project window, specify a project name of CreditCardJavaAgent.
- In the Solution Project field, ensure
that the
MyCreditCardSolutionproject is selected. - In the Agent Name field, leave the default value of MyAgent. This name becomes the name of the .java file and the Java™ class.
- Click Finish. A new folder is created in the solution explorer, and the Java agent project generates an agent descriptor file, agent.adsc, in which you define the agent. A starting template is provided and the values you must complete are indicated with angle brackets and underlines:

In the agent.adsc file, the default name of the agent is mycreditcardsolution.creditcardjavaagent.MyAgent. The
mycreditcardsolutionpart comes from the solution name and thecreditcardjavaagentpart comes from the agent project name, but in lowercase characters. TheMyAgentpart is the name of the Java class.You complete four parts of the descriptor: entity, event, source, and target. For this agent, the entity is the
Accountthat you defined in the business model. The priority of this agent isLow, which means that if more than one agent reacts to the transaction event, this agent is called after other agents that have a higher priority. The event is theTransactionbusiness event that you defined in the business model. For thewherecondition of the transaction, you want to indicate that the account is specified in the transaction event.In this tutorial, the final agent must be as follows:
- Complete the agent descriptor to match the preceding image
and click Save. Instead of using the completion menus, you can copy and paste the following code:
'mycreditcardsolution.creditcardjavaagent.MyAgent' is an agent related to an account, whose priority is Low, processing events : - transaction , where this account comes from the account of this transactionAfter the agent descriptor is complete, you can add the code to the agent.
- In the Solution Explorer view, open
the MyAgent.java file, which now exists in the CreditCardJavaAgent/src/mycreditcardsolution/creditcardjavaagent folder.
The Java file by default contains some of the code you need, including a package statement, some common import commands, and the basic structure for the MyAgent class.
-
Copy the following Java code into the
MyAgent.java file.
Important: Replace all the default code in the file, including the
packageandimportstatements.package mycreditcardsolution.creditcardjavaagent; import com.ibm.ia.agent.EntityAgent; import com.ibm.ia.common.AgentException; import com.ibm.ia.model.Event; import creditcard.Account; import creditcard.Transaction; public class MyAgent extends EntityAgent<Account> { @Override public void process(Event event) throws AgentException { if (event instanceof Transaction) { Account account = getBoundEntity(); String accountId = account.getId(); String url = getSolutionProperty("getting_started_external_service_url"); printToLog("Invoking external service at url: " + url + " for account with id: " + accountId); } } } - Click Save.
- Review the Java code
so that you understand at least in general what it does.
The code gets the bound
accountentity and the URL that is provided as a solution property to print a log message to simulate the call to an external service. Theimport creditcard.*statements are required for the events and entities that are defined in the model and referred to in the code. - Because the getSolutionProperty method
in the Java code gets a property
value, you must define the property in the solution project:
- In the Solution Explorer view, expand the MyCreditCardSolution project.
- Double-click the solution_properties.xml file, then click the Source tab.
- Add the following line to the file. Ensure that you
add it outside of the XML comment tags:
<property name="getting_started_external_service_url">external service url</property>The following image shows the line added at the end of the file:
- Click Save and close the properties file.
- Close the MyAgent.java file and the agent.adsc file.