mirror of https://github.com/halo-dev/halo
Remove deprecated classes
parent
77196780ee
commit
1daead943d
|
@ -1,85 +0,0 @@
|
|||
package run.halo.app.event;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
|
||||
import javax.annotation.PreDestroy;
|
||||
import java.util.EventListener;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
/**
|
||||
* Event queue dispatcher.
|
||||
*
|
||||
* @author johnniang
|
||||
* @date 19-4-20
|
||||
*/
|
||||
@Slf4j
|
||||
@Deprecated
|
||||
public class ApplicationEventQueuePublisher {
|
||||
|
||||
private final BlockingQueue<Object> events = new LinkedBlockingQueue<>();
|
||||
|
||||
private final ApplicationListenerManager listenerManager;
|
||||
|
||||
private final ExecutorService executorService;
|
||||
|
||||
public ApplicationEventQueuePublisher(ApplicationListenerManager listenerManager) {
|
||||
this.listenerManager = listenerManager;
|
||||
this.executorService = Executors.newSingleThreadExecutor();
|
||||
|
||||
executorService.execute(new EventQueueConsumer());
|
||||
}
|
||||
|
||||
public void publishEvent(Object event) {
|
||||
try {
|
||||
events.put(event);
|
||||
} catch (InterruptedException e) {
|
||||
log.warn("Failed to put event to the queue", e);
|
||||
// Ignore this error
|
||||
}
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
protected void destroy() {
|
||||
log.info("Shutting down all event queue consumer");
|
||||
this.executorService.shutdownNow();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private class EventQueueConsumer implements Runnable {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
while (!Thread.currentThread().isInterrupted()) {
|
||||
try {
|
||||
// Take an event
|
||||
Object event = events.take();
|
||||
|
||||
// Get listeners
|
||||
List<EventListener> listeners = listenerManager.getListeners(event);
|
||||
|
||||
// Handle the event
|
||||
listeners.forEach(listener -> {
|
||||
if (listener instanceof ApplicationListener && event instanceof ApplicationEvent) {
|
||||
ApplicationEvent applicationEvent = (ApplicationEvent) event;
|
||||
// Fire event
|
||||
((ApplicationListener) listener).onApplicationEvent(applicationEvent);
|
||||
}
|
||||
});
|
||||
|
||||
log.info("Event queue consumer has been shut down");
|
||||
} catch (InterruptedException e) {
|
||||
log.warn("Failed to take event", e);
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to handle event", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,84 +0,0 @@
|
|||
package run.halo.app.event;
|
||||
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.lang.Nullable;
|
||||
import run.halo.app.utils.ReflectionUtils;
|
||||
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* Application listener manager.
|
||||
*
|
||||
* @author johnniang
|
||||
* @date 19-4-21
|
||||
*/
|
||||
@Slf4j
|
||||
@Deprecated
|
||||
public class ApplicationListenerManager {
|
||||
|
||||
/**
|
||||
* Listener Map.
|
||||
*/
|
||||
private final Map<String, List<EventListener>> listenerMap = new ConcurrentHashMap<>();
|
||||
|
||||
public ApplicationListenerManager(ApplicationContext applicationContext) {
|
||||
// TODO Need to refactor
|
||||
// Register all listener on starting up
|
||||
applicationContext.getBeansOfType(ApplicationListener.class).values().forEach(this::register);
|
||||
|
||||
log.debug("Initialized event listeners");
|
||||
}
|
||||
|
||||
public List<EventListener> getListeners(@Nullable Object event) {
|
||||
if (event == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
// Get listeners
|
||||
List<EventListener> listeners = listenerMap.get(event.getClass().getTypeName());
|
||||
// Clone the listeners
|
||||
return listeners == null ? Collections.emptyList() : new LinkedList<>(listeners);
|
||||
}
|
||||
|
||||
public synchronized void register(@NonNull ApplicationListener<?> listener) {
|
||||
// Get actual generic type
|
||||
Type actualType = resolveActualGenericType(listener);
|
||||
|
||||
if (actualType == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Add this listener
|
||||
listenerMap.computeIfAbsent(actualType.getTypeName(), (key) -> new LinkedList<>()).add(listener);
|
||||
}
|
||||
|
||||
public synchronized void unRegister(@NonNull ApplicationListener<?> listener) {
|
||||
// Get actual generic type
|
||||
Type actualType = resolveActualGenericType(listener);
|
||||
|
||||
if (actualType == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Remove it from listener map
|
||||
listenerMap.getOrDefault(actualType.getTypeName(), Collections.emptyList()).remove(listener);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private Type resolveActualGenericType(@NonNull ApplicationListener<?> listener) {
|
||||
Assert.notNull(listener, "Application listener must not be null");
|
||||
|
||||
log.debug("Attempting to resolve type of Application listener: [{}]", listener);
|
||||
|
||||
ParameterizedType parameterizedType = ReflectionUtils.getParameterizedType(ApplicationListener.class, ((ApplicationListener) listener).getClass());
|
||||
|
||||
return parameterizedType == null ? null : parameterizedType.getActualTypeArguments()[0];
|
||||
}
|
||||
}
|
|
@ -126,7 +126,6 @@ public class ContentArchiveController {
|
|||
model.addAttribute("comments", comments);
|
||||
model.addAttribute("pageRainbow", pageRainbow);
|
||||
|
||||
// Log it
|
||||
return themeService.render("post");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue