package edu.ap.spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import edu.ap.spring.controller.RedisController;
import edu.ap.spring.redis.RedisService;

@SpringBootApplication
public class RedisApplication {

    private String CHANNEL = "edu:ap:redis";
    @Autowired
    private RedisService service;

    //Subscript op channel
    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
                                            MessageListenerAdapter listenerAdapter) {

        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.addMessageListener(listenerAdapter, new ChannelTopic(CHANNEL));

        return container;
    }

    //Deze luistert op channel en wanneer er een message komt stuur die deze onMessage functie van Controller
    @Bean
    MessageListenerAdapter listenerAdapter(RedisController controller) {
        return new MessageListenerAdapter(controller, "onMessage");
    }

    //Deze gaat de database leeg maken en een message daarin toevoegen
    @Bean
    public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
        return (args) -> {

            // empty db
            this.service.flushDb();

            // messaging
            service.sendMessage(CHANNEL, "Hello from Spring Boot");
        };
    }

    public static void main(String[] args) {
        SpringApplication.run(RedisApplication.class, args);
    }
}

//Deze gaat de database leeg maken en een message daarin toevoegen
package edu.ap.spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import edu.ap.spring.controller.RedisController;
import edu.ap.spring.redis.RedisService;

@SpringBootApplication
public class RedisApplication {

    private String CHANNEL = "edu:ap:redis";
    @Autowired
    private RedisService service;

    //Subscript op channel
    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
                                            MessageListenerAdapter listenerAdapter) {

        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.addMessageListener(listenerAdapter, new ChannelTopic(CHANNEL));

        return container;
    }

    //Deze luistert op channel en wanneer er een message komt stuur die deze onMessage functie van Controller
    @Bean
    MessageListenerAdapter listenerAdapter(RedisController controller) {
        return new MessageListenerAdapter(controller, "onMessage");
    }

    //Deze gaat<SUF>
    @Bean
    public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
        return (args) -> {

            // empty db
            this.service.flushDb();

            // messaging
            service.sendMessage(CHANNEL, "Hello from Spring Boot");
        };
    }

    public static void main(String[] args) {
        SpringApplication.run(RedisApplication.class, args);
    }
}
