< Previous | Next >

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

  1. In the Solution Map view, click Add Java agent.
    Link to add a Java agent in the Solution Map view
  2. In the New Java Agent Project window, specify a project name of CreditCardJavaAgent.
  3. In the Solution Project field, ensure that the MyCreditCardSolution project is selected.
  4. In the Agent Name field, leave the default value of MyAgent. This name becomes the name of the .java file and the Java™ class.
  5. 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:
    The agent descriptor file for the CreditCardJavaAgent agent descriptor before you complete it

    In the agent.adsc file, the default name of the agent is mycreditcardsolution.creditcardjavaagent.MyAgent. The mycreditcardsolution part comes from the solution name and the creditcardjavaagent part comes from the agent project name, but in lowercase characters. The MyAgent part 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 Account that you defined in the business model. The priority of this agent is Low, 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 the Transaction business event that you defined in the business model. For the where condition 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:

    The descriptor for the CreditCardJavaAgent agent after you complete it

  6. 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 transaction

    After the agent descriptor is complete, you can add the code to the agent.

  7. 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.
  8. Copy the following Java code into the MyAgent.java file.
    Important: Replace all the default code in the file, including the package and import statements.
    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);
          }
       }
    }
  9. Click Save.
  10. Review the Java code so that you understand at least in general what it does.

    The code gets the bound account entity and the URL that is provided as a solution property to print a log message to simulate the call to an external service. The import creditcard.* statements are required for the events and entities that are defined in the model and referred to in the code.

  11. Because the getSolutionProperty method in the Java code gets a property value, you must define the property in the solution project:
    1. In the Solution Explorer view, expand the MyCreditCardSolution project.
    2. Double-click the solution_properties.xml file, then click the Source tab.
    3. 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:

      Property added to the solution_properties.xml file

    4. Click Save and close the properties file.
  12. Close the MyAgent.java file and the agent.adsc file.
< Previous | Next >