Within the Interlok framework, an AdaptrisMessageProducer is responsible for sending the message to the target system. If the target system supports request-reply then extend RequestReplyProducerImp otherwise extend ProduceOnlyProducerImp

  • You can get access to the configured AdaptrisConnection instance by using the retrieveConnection method.
  • Call this.encode(AdaptrisMessage) to encode the message with any configured AdaptrisMessageEncoder implementation (optional).

Example

Our previous example of a Connection defined our connection; now we need to work with the ClientConnection interface:

public interface ClientConnection {
  public boolean sendMessage(String to, byte[] message) throws IOException;
}

Our AdaptrisMessageProducer implementation can use ClientConnection to send messages.

@XStreamAlias("my-client-producer")
public class MyClientProducer extends ProduceOnlyProducerImp {

  public MyClientProducer() {
  }

  @Override
  public void init() throws CoreException {
  }

  @Override
  public void start() throws CoreException {
  }

  @Override
  public void stop() {
  }

  @Override
  public void close() {
  }

  @Override
  public void prepare() throws CoreException {}

  public void produce(AdaptrisMessage m, ProduceDestination d) throws ProduceException {
    try {
      String to = getDestination().getDestination(m);
      ClientConnection conn = retrieveConnection(MyClientConnection.class).createConnection();
      conn.sendMessage(to, this.encode(m));
    }
    catch (Exception e) {
      throw new ProduceException(e);
    }
  }

So, the summary of what we did is as follows :

  • The lifecycle methods do nothing; this pre-supposes that ClientConnection is pretty lightweight and can be disposed of via garbage collection.
  • We can figure out where we are sending messages to from the ProduceDestination implementation.
  • We call retrieveConnection to find the configured connection object.
  • We catch and throw a ProduceException to trigger error-handling behaviour.
  • We call this.encode(AdaptrisMessage) which encodes the message (or not).
  • There is no additional configuration required.
  • An @XStreamAlias is added so that we have an alias that we can configure; so now, configuration is <producer class="my-client-producer"/> rather than the fully qualified classname.
Tags: developer