Contact Us 1-800-596-4880

Context Information Injection

To get information about the context in which an operation or source is being processed, you can add parameters to the operation or source. The parameters do not appear in the UI, but they are injected by the SDK into the runtime.

Component Location

When executing an operation or using a source, you might want to have some context about the location of the component, mostly for logging purposes. To achieve this, you can define a ComponentLocation as a parameter, and it will be injected.

This examples shows how to log the component location when starting a source:

@Alias("listener")
public class HttpListener extends Source<InputStream, HttpRequestAttributes> {

  private static final Logger LOGGER = getLogger(HttpListener.class);

  @Connection
  private ConnectionProvider<HttpServer> serverProvider;

  private ComponentLocation location;

  @Parameter
  private String path;

  private HttpServer server;

  @Override
  public void onStart(SourceCallback<InputStream, HttpRequestAttributes> sourceCallback) throws MuleException {
    server = serverProvider.connect();
    if (LOGGER.isDebugEnabled()) {
      LOGGER.debug("Starting HTTP Listener %s on path %s", location.getLocation(), path);
    }
    server.listen(path);
  }
// ...
}

This example shows how to log the component location in an operations (non-source) context:

//ComponentLocation is added as a parameter when used in an operation
  public String sayHi(String person, String q, ComponentLocation location) {
    // ...

    System.out.println("Location: " + location.getLocation());

   // ...
  }

Tracking an Event

Available since version 1.1

To be able to trace an event through logs, you can use a CorrelationInfo parameter in your operations and source callback. This correlation info provides the event ID and correlation ID of the event that is processed.

You simply need to add a CorrelationInfo parameter to your operation method or source callback. The instance will be populated with the event information the method or callback runs.

This simplified example shows how to use CorrelationInfo:

public class ObjectStoreOperations {
  private static final Logger LOGGER = getLogger(ObjectStoreOperations.class);
// ...
  @Throws(StoreErrorTypeProvider.class)
  public void store(String key,
                    @Content TypedValue<Serializable> value,
                    ObjectStore objectStore,
                    CorrelationInfo correlationInfo) {
      objectStore.store(key, value);
      if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Object stored with key: %s .The event id is: %s", key, correlationInfo.getEventId());
      }
  }
// ...
}