Free MuleSoft CONNECT Keynote & Expo Pass Available!

Register now+
Nav

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:


         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
@Alias("listener")
public class HttpListener extends Source<InputStream, HttpRequestAttributes> {

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

  @Connection
  private ConnectionProvider<HttpServer> serverProvider;

  @Parameter
  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);
  }
// ...
}

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:


         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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());
      }
  }
// ...
}