[AB-203] using task executors for the async listeners

fixing starboard post listener and service method
This commit is contained in:
Sheldan
2021-03-21 21:00:21 +01:00
parent ebed1daadc
commit 5eefc3909e
28 changed files with 305 additions and 58 deletions

View File

@@ -77,6 +77,59 @@ public class ListenerExecutorConfig {
return executorService.setupExecutorFor("reactionClearedListener");
}
@Bean(name = "aChannelCreatedExecutor")
public TaskExecutor aChannelCreatedExecutor() {
return executorService.setupExecutorFor("aChannelCreatedListener");
}
@Bean(name = "aChannelDeletedExecutor")
public TaskExecutor aChannelDeletedExecutor() {
return executorService.setupExecutorFor("aChannelDeletedListener");
}
@Bean(name = "aRoleCreatedExecutor")
public TaskExecutor aRoleCreatedExecutor() {
return executorService.setupExecutorFor("aRoleCreatedListener");
}
@Bean(name = "aRoleDeletedExecutor")
public TaskExecutor aRoleDeletedExecutor() {
return executorService.setupExecutorFor("aRoleDeletedListener");
}
@Bean(name = "channelGroupCreatedExecutor")
public TaskExecutor channelGroupCreatedExecutor() {
return executorService.setupExecutorFor("channelGroupCreatedListener");
}
@Bean(name = "channelGroupDeletedExecutor")
public TaskExecutor channelGroupDeletedExecutor() {
return executorService.setupExecutorFor("channelGroupDeletedListener");
}
@Bean(name = "serverJoinExecutor")
public TaskExecutor serverJoinExecutor() {
return executorService.setupExecutorFor("serverJoinListener");
}
@Bean(name = "roleCreatedExecutor")
public TaskExecutor roleCreatedExecutor() {
return executorService.setupExecutorFor("roleCreatedListener");
}
@Bean(name = "roleDeletedExecutor")
public TaskExecutor roleDeletedExecutor() {
return executorService.setupExecutorFor("roleDeletedListener");
}
@Bean(name = "channelCreatedExecutor")
public TaskExecutor channelCreatedExecutor() {
return executorService.setupExecutorFor("channelCreatedListener");
}
@Bean(name = "channelDeletedExecutor")
public TaskExecutor channelDeletedExecutor() {
return executorService.setupExecutorFor("channelDeletedListener");
}
}

View File

@@ -11,6 +11,8 @@ import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.events.channel.text.TextChannelCreateEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionalEventListener;
@@ -33,6 +35,10 @@ public class AsyncAChannelCreatedListenerBean extends ListenerAdapter {
@Autowired
private ChannelManagementService channelManagementService;
@Autowired
@Qualifier("aChannelCreatedExecutor")
private TaskExecutor channelCreatedExecutor;
@Override
public void onTextChannelCreate(@Nonnull TextChannelCreateEvent event) {
log.info("Creating text channel with ID {}.", event.getChannel().getIdLong());
@@ -45,7 +51,7 @@ public class AsyncAChannelCreatedListenerBean extends ListenerAdapter {
@TransactionalEventListener
public void executeServerCreationListener(AChannelCreatedListenerModel model) {
if(channelListener == null) return;
channelListener.forEach(serverCreatedListener -> listenerService.executeListener(serverCreatedListener, model));
channelListener.forEach(serverCreatedListener -> listenerService.executeListener(serverCreatedListener, model, channelCreatedExecutor));
}
}

View File

@@ -7,6 +7,8 @@ import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.events.channel.text.TextChannelDeleteEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionalEventListener;
@@ -26,6 +28,10 @@ public class AsyncAChannelDeletedListenerBean extends ListenerAdapter {
@Autowired
private ChannelManagementService channelManagementService;
@Autowired
@Qualifier("aChannelDeletedExecutor")
private TaskExecutor channelDeletedExecutor;
@Override
public void onTextChannelDelete(@Nonnull TextChannelDeleteEvent event) {
channelManagementService.markAsDeleted(event.getChannel().getIdLong());
@@ -34,7 +40,7 @@ public class AsyncAChannelDeletedListenerBean extends ListenerAdapter {
@TransactionalEventListener
public void executeServerCreationListener(AChannelDeletedListenerModel model) {
if(channelDeletedListeners == null) return;
channelDeletedListeners.forEach(serverCreatedListener -> listenerService.executeListener(serverCreatedListener, model));
channelDeletedListeners.forEach(serverCreatedListener -> listenerService.executeListener(serverCreatedListener, model, channelDeletedExecutor));
}
}

View File

@@ -9,6 +9,8 @@ import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.events.role.RoleCreateEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionalEventListener;
@@ -31,6 +33,10 @@ public class AsyncARoleCreatedListenerBean extends ListenerAdapter {
@Autowired
private RoleManagementService roleManagementService;
@Autowired
@Qualifier("aRoleCreatedExecutor")
private TaskExecutor roleCreatedExecutor;
@Override
public void onRoleCreate(@Nonnull RoleCreateEvent event) {
AServer server = serverManagementService.loadServer(event.getGuild());
@@ -40,7 +46,7 @@ public class AsyncARoleCreatedListenerBean extends ListenerAdapter {
@TransactionalEventListener
public void executeServerCreationListener(ARoleCreatedListenerModel model) {
if(roleCreatedListeners == null) return;
roleCreatedListeners.forEach(asyncServerCreatedListener -> listenerService.executeListener(asyncServerCreatedListener, model));
roleCreatedListeners.forEach(asyncServerCreatedListener -> listenerService.executeListener(asyncServerCreatedListener, model, roleCreatedExecutor));
}
}

View File

@@ -7,6 +7,8 @@ import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.events.role.RoleDeleteEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionalEventListener;
@@ -26,6 +28,10 @@ public class AsyncARoleDeletedListenerBean extends ListenerAdapter {
@Autowired
private ListenerService listenerService;
@Autowired
@Qualifier("aRoleDeletedExecutor")
private TaskExecutor roleDeletedExecutor;
@Override
public void onRoleDelete(@Nonnull RoleDeleteEvent event) {
roleManagementService.markDeleted(event.getRole().getIdLong());
@@ -34,7 +40,7 @@ public class AsyncARoleDeletedListenerBean extends ListenerAdapter {
@TransactionalEventListener
public void executeServerCreationListener(ARoleDeletedListenerModel model) {
if(roleDeletedListeners == null) return;
roleDeletedListeners.forEach(serverCreatedListener -> listenerService.executeListener(serverCreatedListener, model));
roleDeletedListeners.forEach(serverCreatedListener -> listenerService.executeListener(serverCreatedListener, model, roleDeletedExecutor));
}
}

View File

@@ -4,6 +4,8 @@ import dev.sheldan.abstracto.core.listener.ListenerService;
import dev.sheldan.abstracto.core.listener.sync.entity.AsyncChannelGroupCreatedListener;
import dev.sheldan.abstracto.core.models.listener.ChannelGroupCreatedListenerModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionalEventListener;
@@ -17,10 +19,14 @@ public class AsyncChannelGroupCreatedListenerManager {
@Autowired
private ListenerService listenerService;
@Autowired
@Qualifier("channelGroupCreatedExecutor")
private TaskExecutor channelGroupCreatedExecutor;
@TransactionalEventListener
public void executeListener(ChannelGroupCreatedListenerModel createdGroup){
listener.forEach(asyncChannelGroupCreatedListener ->
listenerService.executeListener(asyncChannelGroupCreatedListener, createdGroup)
listenerService.executeListener(asyncChannelGroupCreatedListener, createdGroup, channelGroupCreatedExecutor)
);
}

View File

@@ -4,6 +4,8 @@ import dev.sheldan.abstracto.core.listener.ListenerService;
import dev.sheldan.abstracto.core.listener.sync.entity.AsyncChannelGroupDeletedListener;
import dev.sheldan.abstracto.core.models.listener.ChannelGroupDeletedListenerModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionalEventListener;
@@ -17,10 +19,14 @@ public class AsyncChannelGroupDeletedListenerManager {
@Autowired
private ListenerService listenerService;
@Autowired
@Qualifier("channelGroupDeletedExecutor")
private TaskExecutor channelGroupDeletedExecutor;
@TransactionalEventListener
public void executeListener(ChannelGroupDeletedListenerModel model){
listener.forEach(channelGroupCreatedListener ->
listenerService.executeListener(channelGroupCreatedListener, model)
listenerService.executeListener(channelGroupCreatedListener, model, channelGroupDeletedExecutor)
);
}

View File

@@ -7,6 +7,8 @@ import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.events.guild.GuildJoinEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.event.TransactionalEventListener;
@@ -27,6 +29,10 @@ public class AsyncServerJoinListenerBean extends ListenerAdapter {
@Autowired
private ListenerService listenerService;
@Autowired
@Qualifier("serverJoinExecutor")
private TaskExecutor serverJoinExecutor;
@Override
@Transactional
public void onGuildJoin(@Nonnull GuildJoinEvent event) {
@@ -38,7 +44,7 @@ public class AsyncServerJoinListenerBean extends ListenerAdapter {
@TransactionalEventListener
public void executeServerCreationListener(ServerCreatedListenerModel model) {
if(serverCreatedListeners == null) return;
serverCreatedListeners.forEach(asyncServerCreatedListener -> listenerService.executeListener(asyncServerCreatedListener, model));
serverCreatedListeners.forEach(asyncServerCreatedListener -> listenerService.executeListener(asyncServerCreatedListener, model, serverJoinExecutor));
}
}

View File

@@ -6,6 +6,8 @@ import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.events.role.RoleCreateEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Component;
import javax.annotation.Nonnull;
@@ -21,11 +23,16 @@ public class AsyncRoleCreatedListenerBean extends ListenerAdapter {
@Autowired
private ListenerService listenerService;
@Autowired
@Qualifier("roleCreatedExecutor")
private TaskExecutor roleCreatedExecutor;
@Override
public void onRoleCreate(@Nonnull RoleCreateEvent event) {
if(listenerList == null) return;
RoleCreatedModel model = getModel(event);
listenerList.forEach(roleCreatedListener -> listenerService.executeFeatureAwareListener(roleCreatedListener, model));
listenerList.forEach(roleCreatedListener -> listenerService.executeFeatureAwareListener(roleCreatedListener, model, roleCreatedExecutor));
}
private RoleCreatedModel getModel(RoleCreateEvent event) {

View File

@@ -6,6 +6,8 @@ import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.events.role.RoleDeleteEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Component;
import javax.annotation.Nonnull;
@@ -21,11 +23,15 @@ public class AsyncRoleDeletedListenerBean extends ListenerAdapter {
@Autowired
private ListenerService listenerService;
@Autowired
@Qualifier("roleDeletedExecutor")
private TaskExecutor roleDeletedExecutor;
@Override
public void onRoleDelete(@Nonnull RoleDeleteEvent event) {
if(listenerList == null) return;
RoleDeletedModel model = getModel(event);
listenerList.forEach(roleCreatedListener -> listenerService.executeFeatureAwareListener(roleCreatedListener, model));
listenerList.forEach(roleCreatedListener -> listenerService.executeFeatureAwareListener(roleCreatedListener, model, roleDeletedExecutor));
}
private RoleDeletedModel getModel(RoleDeleteEvent event) {

View File

@@ -6,6 +6,8 @@ import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.events.channel.text.TextChannelCreateEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Service;
import javax.annotation.Nonnull;
@@ -21,11 +23,15 @@ public class AsyncTextChannelCreatedListenerBean extends ListenerAdapter {
@Autowired
private ListenerService listenerService;
@Autowired
@Qualifier("channelCreatedExecutor")
private TaskExecutor channelCreatedExecutor;
@Override
public void onTextChannelCreate(@Nonnull TextChannelCreateEvent event) {
if(listenerList == null) return;
TextChannelCreatedModel model = getModel(event);
listenerList.forEach(textChannelCreatedListener -> listenerService.executeFeatureAwareListener(textChannelCreatedListener, model));
listenerList.forEach(textChannelCreatedListener -> listenerService.executeFeatureAwareListener(textChannelCreatedListener, model, channelCreatedExecutor));
}
private TextChannelCreatedModel getModel(TextChannelCreateEvent event) {

View File

@@ -6,6 +6,8 @@ import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.events.channel.text.TextChannelDeleteEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Service;
import javax.annotation.Nonnull;
@@ -21,11 +23,16 @@ public class AsyncTextChannelDeletedListenerBean extends ListenerAdapter {
@Autowired
private ListenerService listenerService;
@Autowired
@Qualifier("channelDeletedExecutor")
private TaskExecutor channelDeletedExecutor;
@Override
public void onTextChannelDelete(@Nonnull TextChannelDeleteEvent event) {
if(listenerList == null) return;
TextChannelDeletedModel model = getModel(event);
listenerList.forEach(textChannelCreatedListener -> listenerService.executeFeatureAwareListener(textChannelCreatedListener, model));
listenerList.forEach(textChannelCreatedListener -> listenerService.executeFeatureAwareListener(textChannelCreatedListener, model, channelDeletedExecutor));
}
private TextChannelDeletedModel getModel(TextChannelDeleteEvent event) {