---
schema_version: "1"
id: 975274daaabc26972e107202fb01fbee42cbecb213aa392db2d11215f060f7f9
title: Obtaining the Config and Connection in a Message Source
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/mule-sdk/latest/sources-config-connection.md
canonical_url: https://docs.mulesoft.com/mule-sdk/latest/sources-config-connection
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 2d75bedb849b67cbd2c699c70c0f958fe36f5faae9a0c1f569db3b4d896ddaa3
description: MuleSoft Documentation Site
release: "1.13"
latest: true
---
---
product: Mule SDK
version: 1.13
is-latest-version: true
keywords: mule, sdk, sources, listener, triggers, config, connection
---

# Obtaining the Config and Connection in a Message Source

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

## Obtaining the Config Object

Just like operations, a source might reference a configuration to obtain parameters about its
behavior. To obtain such a configuration, a field must be annotated with the `@Config` annotation
(the same used in operation arguments):

```java
public class HttpListener extends Source<InputStream, HttpRequestAttributes> {

  @Config
  private HttpListenerConfig config;
}
```

## Obtaining a Connection

If the source requires a connection (and 99% of connectors do unless you are implementing a
Scheduler), you need to obtain a connection. As with operations, the `@Connection` annotation is
used but with two major differences:

- It is used in a field instead of a method argument.

- Instead of the connection object, a `ConnectionProvider` is injected.

```java
public class HttpListener extends Source<InputStream, HttpRequestAttributes> {

    @Config
    private HttpListenerConfig config;

    @Connection
    private ConnectionProvider<HttpServer> serverProvider;

    @Parameter
    @Optional(defaultValue = "/")
    private String path;

    private HttpServer httpServer;

    @Override
    public void onStart(SourceCallback<InputStream, HttpRequestAttributes> sourceCallback) throws MuleException {
        httpServer = serverProvider.connect(); (1)

        httpServer.listen(path).onRequest(request -> { (2)
            processRequest(request, sourceCallback); (3)
        });
    }

    @Override
    public void onStop() {
        if (httpServer != null) { (4)
            serverProvider.disconnect(httpServer); (5)
        }
    }
}
```

<table><tbody><tr><td><i data-value="1"></i><b>1</b></td><td>The example uses the <code>ConnectionProvider</code> to obtain an <code>HttpServer</code>. The typical connection provider has already configured the server with the proper host and port.</td></tr><tr><td><i data-value="2"></i><b>2</b></td><td>HTTP requests are pushed into the server by the remote clients, and the <code>HttpServer</code> component notifies the source of every request. The source then uses the <code>SourceCallback</code> to push the message into the flow.</td></tr><tr><td><i data-value="3"></i><b>3</b></td><td>The source should define the <code>processRequest</code> method to transform the HTTP request into a message that can be pushed to the flow. The documentation will show how to do that later, but to keep the example simple, you can assume that this works.</td></tr><tr><td><i data-value="4"></i><b>4</b></td><td>The <code>onStop()</code> method releases resources, in this case, the <code>HttpServer</code>. It checks for null in case the <code>onStart()</code> method fails before the server is created.</td></tr><tr><td><i data-value="5"></i><b>5</b></td><td>The example uses the <code>disconnect()</code> method of the <code>ConnectionProvider</code> to release the server.</td></tr></tbody></table>

> [!TIP] The example above is pseudocode. It oversimplifies the real HTTP connector to keep the
> example clear and concise.

## Next Step

- [Pushing Messages to a Flow from a Message Source](https://docs.mulesoft.com/mule-sdk/latest/sources-push-message)
