0 / 0
Using JavaBeans, Java Integration Stage

Using JavaBeans

The Java™ Integration stage API supports JavaBeans that allow the Java Integration stage to access existing Java code.

The Java Integration stage assumes the following JavaBeans conventions:
  • The class must have a public default constructor(no-argument).
  • The class must have getter and setter for each property.
The following types of JavaBeans property are supported by the Java Integration stage:
  • Boolean/java.lang.Boolean
  • byte[]
  • short/java.lang.Short
  • int/java.lang.Integer
  • long/java.lang.Long
  • float/java.lang.Float
  • double/java.lang.Double
  • java.lang.String
  • java.math.BigInteger
  • java.math.BigDecimal
  • java.sql.Time
  • java.sql.Timestamp
  • java.sql.Date

The following example shows how to use thesamples.InputBean class for an input link and thesamples.OutputBean class for an output link.

JavaBeansTransformer.java
package samples;

import com.ibm.is.cc.javastage.api.*;

public class JavaBeansTransformer extends Processor
{
  private InputLink m_inputLink;
  private OutputLink m_outputLink;
  private OutputLink m_rejectLink;

  public Capabilities getCapabilities()
  {
    Capabilities capabilities = new Capabilities();
    // Set minimum number of input links to 1
    capabilities.setMinimumInputLinkCount(1);
    // Set maximum number of input links to 1
    capabilities.setMaximumInputLinkCount(1);
    // Set minimum number of output stream links to 1
    capabilities.setMinimumOutputStreamLinkCount(1);
    // Set maximum number of output stream links to 1
    capabilities.setMaximumOutputStreamLinkCount(1);
    // Set maximum number of reject links to 1
    capabilities.setMaximumRejectLinkCount(1);
    // Set is Wave Generator to false
    capabilities.setIsWaveGenerator(false);
    return capabilities;
  }

  public boolean validateConfiguration(
    Configuration configuration, boolean isRuntime)
    throws Exception
  {
    // Specify current link configurations.
    m_inputLink = configuration.getInputLink(0);
    m_outputLink = configuration.getOutputLink(0);
    if (configuration.getRejectLinkCount() == 1)
    {
      m_rejectLink = m_inputLink.getAssociatedRejectLink();
    }

    return true;
  }

  public void process() throws Exception
  {
    OutputRecord outputRecord = m_outputLink.getOutputRecord();

    // Loop until there is no more input data
    do
    {
      InputRecord record = m_inputLink.readRecord();
      if (record == null)
      {
        // End of data
        break;
      }

      // Get the object from the input row.
      InputBean inputBean = (InputBean) record.getObject();

      // Get the value from name column of the input link.
      // If the value contains "*" character, mark reject flag 
      // and send the record
      // to reject link in later proceessing.
      boolean fReject = false;
      String name = inputBean.getFirstName();
      if ((name == null) || (name.indexOf('*') >= 0))
      {
        fReject = true;
      }

      if (!fReject)
      {
        // Send record to output
        OutputBean outputBean = new OutputBean();
        outputBean.setEmpno(inputBean.getEmpno());
        outputBean.setFirstName(inputBean.getFirstName().toUpperCase());
        outputBean.setLastName(inputBean.getLastName().toUpperCase());
        outputBean.setHireDate(inputBean.getHireDate());
        outputBean.setEdLevel(inputBean.getEdLevel());
        outputBean.setSalary(inputBean.getSalary());
        outputBean.setBonus(inputBean.getBonus());
        outputBean.setLastUpdate(inputBean.getLastUpdate());
        outputRecord.putObject(outputBean);
        m_outputLink.writeRecord(outputRecord);
      }
      else if (m_rejectLink != null)
      {
        // Reject record.  This transfers the row to the reject link.
        // The same kind of forwarding is also possible for regular stream
        // links.
        RejectRecord rejectRecord = m_rejectLink.getRejectRecord(record);

        // Reject record can contain additional columns "ERRORTEXT" and "ERRORCODE".
        // The field will be shown as columns in rejected output records.
        rejectRecord.setErrorText("Name field contains *");
        rejectRecord.setErrorCode(123);
        m_rejectLink.writeRecord(rejectRecord);
      }
    }
    while (true);
  }
   
  public Class<InputBean> getBeanForInput(Link inputLink)
  {
    return InputBean.class;
  }

  public Class<OutputBean> getBeanForOutput(Link outputLink)
  {
    return OutputBean.class;
  }
}
InputBean.java

package samples;

import java.sql.Date;
import java.sql.Time;

public class InputBean
{
  private long      m_empno;
  private String    m_firstname;
  private String    m_lastname;
  private Date      m_hiredate;
  private int       m_edlevel;
  private Double    m_salary;
  private double    m_bonus;
  private Time      m_lastupdate;

  /**
   * Fetches the value of the empno field.
   *
   * @return long value of empno field
   */
  public long getEmpno()
  {
    return m_empno;
  }

  /**
   * Set the value of the empno field.
   *
   * @param empno value of the empno field.
   */
  public void setEmpno(long empno)
  {
    m_empno = empno;
  }

  /**
   * Fetches the value of the firstname field.
   *
   * @return String value of firstname field
   */
  public String getFirstName()
  {
    return m_firstname;
  }

  /**
   * Set the value of the firstname field.
   *
   * @param firstname value of the firstname field.
   */
  public void setFirstName(String firstname)
  {
    m_firstname = firstname;
  }

  /**
   * Fetches the value of the lastname field.
   *
   * @return String value of lastname field
   */
  public String getLastName()
  {
    return m_lastname;
  }

  /**
   * Set the value of the lastname field.
   *
   * @param lastname value of the lastname field.
   */
  public void setLastName(String lastname)
  {
    m_lastname = lastname;
  }

  /**
   * Fetches the value of the hiredate field.
   *
   * @return Date value of hiredate field
   */
  public Date getHireDate()
  {
    return m_hiredate;
  }

  /**
   * Set the value of the hiredate field.
   *
   * @param hiredate value of the hiredate field.
   */
  public void setHireDate(Date hiredate)
  {
    m_hiredate = hiredate;
  }

  /**
   * Fetches the value of the edlevel field.
   *
   * @return int value of edlevel field
   */
  public int getEdLevel()
  {
    return m_edlevel;
  }

  /**
   * Set the value of the edlevel field.
   *
   * @param edlevel value of the edlevel field.
   */
  public void setEdLevel(int edlevel)
  {
    m_edlevel = edlevel;
  }

  /**
   * Fetches the value of the salary field.
   *
   * @return Double value of salary field
   */
  public Double getSalary()
  {
    return m_salary;
  }

  /**
   * Set the value of the salary field.
   *
   * @param salary value of the salary field.
   */
  public void setSalary(Double salary)
  {
    m_salary = salary;
  }

  /**
   * Fetches the value of the bonus field.
   *
   * @return double value of bonus field
   */
  public double getBonus()
  {
    return m_bonus;
  }

  /**
   * Set the value of the bonus field.
   *
   * @param bonus value of the bonus field.
   */
  public void setBonus(double bonus)
  {
    m_bonus = bonus;
  }

  /**
   * Fetches the value of the lastupdate field.
   *
   * @return Time value of lastupdate field
   */
  public Time getLastUpdate()
  {
    return m_lastupdate;
  }

  /**
   * Set the value of the lastupdate field.
   *
   * @param lastupdate value of the lastupdate field.
   */
  public void setLastUpdate(Time lastupdate)
  {
    m_lastupdate = lastupdate;
  }
}
OutputBean.java

package samples;

import java.sql.Date;
import java.sql.Time;

public class OutputBean
{
  private long      m_empno;
  private String    m_firstname;
  private String    m_lastname;
  private Date      m_hiredate;
  private int       m_edlevel;
  private Double    m_salary;
  private double    m_bonus;
  private double    m_income;
  private Time      m_lastupdate;

  /**
   * Fetches the value of the empno field.
   *
   * @return long value of empno field
   */
  public long getEmpno()
  {
    return m_empno;
  }

  /**
   * Set the value of the empno field.
   *
   * @param empno value of the empno field.
   */
  public void setEmpno(long empno)
  {
    m_empno = empno;
  }

  /**
   * Fetches the value of the firstname field.
   *
   * @return String value of firstname field
   */
  public String getFirstName()
  {
    return m_firstname;
  }

  /**
   * Set the value of the firstname field.
   *
   * @param firstname value of the firstname field.
   */
  public void setFirstName(String firstname)
  {
    m_firstname = firstname;
  }

  /**
   * Fetches the value of the lastname field.
   *
   * @return String value of lastname field
   */
  public String getLastName()
  {
    return m_lastname;
  }

  /**
   * Set the value of the lastname field.
   *
   * @param lastname value of the lastname field.
   */
  public void setLastName(String lastname)
  {
    m_lastname = lastname;
  }

  /**
   * Fetches the value of the hiredate field.
   *
   * @return Date value of hiredate field
   */
  public Date getHireDate()
  {
    return m_hiredate;
  }

  /**
   * Set the value of the hiredate field.
   *
   * @param hiredate value of the hiredate field.
   */
  public void setHireDate(Date hiredate)
  {
    m_hiredate = hiredate;
  }

  /**
   * Fetches the value of the edlevel field.
   *
   * @return int value of edlevel field
   */
  public int getEdLevel()
  {
    return m_edlevel;
  }

  /**
   * Set the value of the edlevel field.
   *
   * @param edlevel value of the edlevel field.
   */
  public void setEdLevel(int edlevel)
  {
    m_edlevel = edlevel;
  }

  /**
   * Fetches the value of the salary field.
   *
   * @return Double value of salary field
   */
  public Double getSalary()
  {
    return m_salary;
  }

  /**
   * Set the value of the salary field.
   *
   * @param salary value of the salary field.
   */
  public void setSalary(Double salary)
  {
    m_salary = salary;
  }

  /**
   * Fetches the value of the bonus field.
   *
   * @return double value of bonus field
   */
  public double getBonus()
  {
    return m_bonus;
  }

  /**
   * Set the value of the bonus field.
   *
   * @param bonus value of the bonus field.
   */
  public void setBonus(double bonus)
  {
    m_bonus = bonus;
  }

  /**
   * Fetches the value of the lastupdate field.
   *
   * @return Time value of lastupdate field
   */
  public Time getLastUpdate()
  {
    return m_lastupdate;
  }

  /**
   * Set the value of the lastupdate field.
   *
   * @param lastupdate value of the lastupdate field.
   */
  public void setLastUpdate(Time lastupdate)
  {
    m_lastupdate = lastupdate;
  }
}
If your Java code uses JavaBeans as representations of records on the link, you must override the getBeanForInput() and the getBeanforOutput() methods in the Processor class. Your Java code must return the java.lang.Class of JavaBeans class corresponding to each link. The Java Integration stage will invoke these method at the time of initialization.
public Class<InputBean> getBeanForInput(Link inputLink)
{
  return InputBean.class;
}

public Class<OutputBean> getBeanForOutput(Link outputLink)
{
  return OutputBean.class;
The JavaBeans class corresponding to an input link can be instantiated by the Java Integration stage, and your Java code can get this instance by calling the getObject() method of the InputLink interface.
InputBean inputBean = (InputBean) record.getObject();

      
String name = inputBean.getFirstName();
      :
      :
Before your Java code writes a record to an output link which is associated with JavaBeans, your Java code must instantiate a JavaBeans object for the output link, and set the value for each bean properties. The type of JavaBeans object needs to match the type specified by the getBeanForOutput() method.
 // Send record to output
 OutputBean outputBean = new OutputBean();
 outputBean.setEmpno(inputBean.getEmpno());
 outputBean.setFirstName(inputBean.getFirstName().toUpperCase());
 outputBean.setLastName(inputBean.getLastName().toUpperCase());
 outputBean.setHireDate(inputBean.getHireDate());
 outputBean.setEdLevel(inputBean.getEdLevel());
 outputBean.setSalary(inputBean.getSalary());
 outputBean.setBonus(inputBean.getBonus());
 outputBean.setLastUpdate(inputBean.getLastUpdate());
Finally, your code must call the putObject(Object) method of theOutputRecord interface to set this JavaBeans object to the output record.
outputRecord.putObject(outputBean);
Generative AI search and answer
These answers are generated by a large language model in watsonx.ai based on content from the product documentation. Learn more