# @Producer Reducir Repository

Permite definir @Producer para inyectar MongoClient y reducir el codigo del repositorio

![](/files/-Lc1X7Fft-GfUs3NpkQC)

**JMoordbProducer.java**

* Se encuentra en el repositorio
* @Producer indica que cuando se inyecte en una clase  @Inject MongoClient mongoClient el invocara el valor devuelto por el método.
* Como se define de tipo @Singleton este solo se ejecutara una vez.

```java
@Singleton
@ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)
public class JmoordbProducer {

    @Produces
    @ApplicationScoped
    public MongoClient mongoClient() {
        MongoClient mongo = new MongoClient("localhost", 27017);
        try {
            String username = (String) JmoordbContext.get("username");
            String password = (String) JmoordbContext.get("password");
            String database = (String) JmoordbContext.get("database");
            String host = (String) JmoordbContext.get("host");
            String port = (String) JmoordbContext.get("port");
            Boolean security = (Boolean) JmoordbContext.get("security");
            if (security == null && username == null) {
                security = false;
            }
            if (security) {

            char[] charArray = password.toCharArray(); 
            MongoCredential credential = MongoCredential.createCredential(username, database, charArray);
            ServerAddress serverAddress = new ServerAddress(host, Integer.parseInt(port));
//             mongo = new MongoClient(serverAddress, new ArrayList<>());

             mongo = new MongoClient(serverAddress, new ArrayList<MongoCredential>() {{ add(credential); }});
//           
            }else{
                mongo = new MongoClient();
            }

        } catch (Exception e) {
            JmoordbUtil.errorMessage("coneecction() "+e.getLocalizedMessage());
        }

        return mongo;
    }
}
```

## Clase principal del proyecto Web

* En el método init() de la clase principal del proyecto Web, definimos una instancia de JmoordConecction
* Esto permite a la interface Repository\<T> invocar el @Producer de la conexión.

```java
 JmoordbConnection  jmoordbConnection = new JmoordbConnection.Builder()
                    .withSecurity(false)                  
                    .withDatabase("")
                    .withHost("")
                    .withPort(0)
                    .withUsername("")
                    .withPassword(password)
                    .build();
```

## Definir un Repository

```java
@Stateless
public class RolRepository extends Repository<Rol> {
    public RolRepository() {
        super(Rol.class, "store", "rol");
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://avbravo-2.gitbook.io/trucosjakartaee/producer-reducir-repository/producer-reducir-repository.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
