Search code examples
javaspring-data-jpaconsumer

Spring Boot JPA - How to use JPA from a Consumer<T>


I am working on an spring boot application that listens to some events through a consumer.

The structure of my app is as follows:

enter image description here

Everything seems to work fine except from when an event is emmited and therefore redirected to the consumer class, i can react to the consumer event but i cannot communicate from there to the ddbb.

Next is my Event entity:

package com.victor.simple.events.entities;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Event {
    @Id
    private String id;
    private String channel;
    private String chaincode;

    private String event;
    private String iteration;
    private String hash;
    private String createdAt;
    private String eventType;
    private String tenantID;

    @Override
    public String toString() {
        return "Event{" +
                "id='" + id + '\'' +
                ", channel='" + channel + '\'' +
                ", chaincode='" + chaincode + '\'' +
                ", event='" + event + '\'' +
                ", iteration='" + iteration + '\'' +
                ", hash='" + hash + '\'' +
                ", createdAt='" + createdAt + '\'' +
                ", eventType='" + eventType + '\'' +
                ", tenantID='" + tenantID + '\'' +
                '}';
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getChannel() {
        return channel;
    }

    public void setChannel(String channel) {
        this.channel = channel;
    }

    public String getChaincode() {
        return chaincode;
    }

    public void setChaincode(String chaincode) {
        this.chaincode = chaincode;
    }

    public String getEvent() {
        return event;
    }

    public void setEvent(String event) {
        this.event = event;
    }

    public String getIteration() {
        return iteration;
    }

    public void setIteration(String iteration) {
        this.iteration = iteration;
    }

    public String getHash() {
        return hash;
    }

    public void setHash(String hash) {
        this.hash = hash;
    }

    public String getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(String createdAt) {
        this.createdAt = createdAt;
    }

    public String getEventType() {
        return eventType;
    }

    public void setEventType(String eventType) {
        this.eventType = eventType;
    }

    public String getTenantID() {
        return tenantID;
    }

    public void setTenantID(String tenantID) {
        this.tenantID = tenantID;
    }

}

Next is my EventRepository:

public interface EventRepository extends JpaRepository<Event,String> {
}

Next is my EventController:

@Slf4j
@RequiredArgsConstructor
@RestController
public class EventController {

    @Autowired
    EventRepository repository;
    private final Gateway networkService;

    @Value("${fabric.channel.contract}")
    private String contractName;

    @Value("${fabric.channel.name}")
    private String channelName;

    @PostConstruct
    private void registerEvent() throws ContractException{
        System.out.println("PostConstruct registerEvent() execution start");
        Network network = networkService.getNetwork("corechannel");
        Contract contract = network.getContract("sample-node-event-victor");


        //Capture contract events
        Consumer<ContractEvent> eventConsumer = new EventConsumer();
        contract.addContractListener(eventConsumer);

    }


}

Next is my consumer class:


@Component
public class EventConsumer implements Consumer<ContractEvent> {

    @Autowired
    EventRepository repository;

    @Override
    public void accept(ContractEvent contractEvent) {
        System.out.println("Leyendo eventos");
        System.out.println("repo es: "+repository);
        //saveEvent();
    }

    void saveEvent() {

        Event event=new Event();
        event.setId("1");
        repository.save(event);

    }
}

Any ideas? When i receive the event the sout("Leyendo eventos") works fine. But then it says that the repository is null..

I also tried by some suggestion to see if the bean was recognized and the test was successfull:

enter image description here


Solution

  • You need to Autowire the EventConsumer in the EventController. If you manually instantiate it (as you are currently doing), the Autowired dependencies cannot be set by Spring.

    @Autowired
    private EventConsumer eventConsumer;