# Eventos

![](/files/-Lc1X7sIk9zXTnD5VOE-)

Cuando ocurre una acciòn por ejemplo vender un articulo, puede que necesitemos realizar otras opciones tales como

crear una orden de entrega, eliminar del inventario.

Para estos casos podemos usar eventos y listener que estarán pendientes de cuando ocurra un evento activarse.

Donde se puede aplicar:

* Por ejemplo cuando vendes un articulo deseas actualizar el inventario de ese articulo.
* Generar una orden de entrega.

Implementar eventos

Deseamos activar eventos cuando ocurre determinada accion.

Por ejemplo si ocurre un evento de eliminar un rol podemos activar el metodo fire() y existen listener escuchando lo que esta ocurriendo con el evento.

**Requisitos:**

* Evento
* Controller
* Listener
* Otros componentes opcional

![](/files/-Lc1X7sKH3-2qMg_-1D6)

## Clase RolEvento.java

```java
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.avbravo.storeejb.eventos;

import com.avbravo.storeejb.entity.Rol;

/**
 *
 * @author avbravo
 */
public class RolEvento {

    private Rol rol;

    public Rol getRol() {
        return rol;
    }

    public void setRol(Rol rol) {
        this.rol = rol;
    }

    public RolEvento() {

    }

    public RolEvento(Rol rol) {
        this.rol = rol;

    }


}
```

## EN EL CONTROLLER

RolController.java

* **definir una lista de Event<>**
* Invocar el metodo fire()

```java
  @Inject
    Event<RolEvento> rolEventos;
```

**Invocar el método fire()**

```java
 rolEventos.fire(new RolEvento(rol));
```

```java
 @Override
    public String delete(Object item, Boolean deleteonviewpage) {
        String path = "";
        try {
            rol = (Rol) item;
            rolEventos.fire(new RolEvento(rol));

}catch(Exception ex){
}
}
```

## ColorListener.java

```java
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.avbravo.storeejb.listener;

import com.avbravo.storeejb.eventos.RolEvento;
import com.avbravo.storeejb.producer.LookupStoreejbServices;
import com.avbravo.storeejb.rules.ColorRules;
import javax.ejb.Stateless;
import javax.enterprise.event.Observes;
import javax.inject.Inject;

/**
 *
 * @author avbravo
 */
@Stateless
public class ColorListener {
     @Inject
     ColorRules colorRules;

      public void escuchaDelete(@Observes RolEvento evento) {
           try {
                  System.out.println("---> escucho en color");
           // rolRules.isDeleted(evento.getRol());
          } catch (Exception e) {
              System.out.println("ColorListener() "+e.getLocalizedMessage());
          }
    }
}
```

## UsuarioListener.java

```java
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.avbravo.storeejb.listener;

import com.avbravo.storeejb.eventos.RolEvento;
import com.avbravo.storeejb.rules.RolRules;
import javax.ejb.Stateless;
import javax.enterprise.event.Observes;
import javax.inject.Inject;

/**
 *
 * @author avbravo
 */
@Stateless
public class UsuarioListener {


    @Inject
    RolRules rolRules;

    public void escuchaDelete(@Observes RolEvento evento) {
        try {
            System.out.println("---> escucho en usuario ");
            System.out.println("----> se asigno en true");
            rolRules.isDeleted(evento.getRol());
            System.out.println("----> se elimino");
        } catch (Exception e) {
            System.out.println("UsuarioListener() " + e.getLocalizedMessage());
        }

    }
}
```


---

# 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/overview/eventos/implementar-eventos-ejb.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.
