> For the complete documentation index, see [llms.txt](https://avbravo-2.gitbook.io/trucosjakartaee/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://avbravo-2.gitbook.io/trucosjakartaee/producer-reducir-repository/producer-reducir-repository.md).

# @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");
    }
}
```
