Files
halo/docs/plugin/shared-event.md
John Niang a94596a9f8 Add support for publishing events among plugins (#6081)
#### What type of PR is this?

/kind feature
/area core
/area plugin

#### What this PR does / why we need it:

This PR enhance usage of SharedEvent annotation to add support for publishing events among plugins.

#### How to test?

1. Clone repository https://github.com/halo-dev/plugin-starter
2. Change build.gradle as following:

	```gradle
	dependencies {
	    implementation platform('run.halo.tools.platform:plugin:2.17.0-SNAPSHOT')
	```

3. Change StarterPlugin as following:

	```java
	@Component
	public class StarterPlugin extends BasePlugin {
	
	    private final ApplicationContext appContext;
	
	    public StarterPlugin(PluginContext pluginContext, ApplicationContext appContext) {
	        super(pluginContext);
	        this.appContext = appContext;
	    }
	
	    @Override
	    public void start() {
	        appContext.publishEvent(new PostDeletedEvent(this, "fake-plugin"));
	    }
	
	    @Override
	    public void stop() {
	    }

	    @EventListener(PostDeletedEvent.class)
	    public void onApplicationEvent(PostDeletedEvent event) {
	        System.out.println("Post deleted event received in plugin: " + event.getName());
	    }
	}
	```
4. Add a listener to Halo core
```java
	    @EventListener(PostDeletedEvent.class)
	    public void onApplicationEvent(PostDeletedEvent event) {
	        System.out.println("Post deleted event received in core: " + event.getName());
	    }
```
5. Build plugin and install plugin
6. Enable the plugin and see the result

#### Does this PR introduce a user-facing change?

```release-note
None
```
2024-06-19 16:11:00 +00:00

2.1 KiB
Raw Permalink Blame History

插件中如何发送共享事件SharedEvent

在插件中可以通过共享事件SharedEvent来发送消息。 共享事件是一种特殊的事件,它可以被核心和所有插件订阅。

订阅共享事件

目前,核心中已经提供了不少的共享事件,例如 run.halo.app.event.post.PostPublishedEventrun.halo.app.event.post.PostUpdatedEvent ,这些事件由核心发布,核心和插件均可订阅。请看下面的示例:


@Component
public class PostPublishedEventListener implements ApplicationListener<PostPublishedEvent> {

  @Override
  public void onApplicationEvent(PostPublishedEvent event) {
    // Do something
  }

}

或者通过 @EventListener 注解实现,


@Component
public class PostPublishedEventListener {

  @EventListener
  // @Async // 如果需要异步处理,可以添加此注解
  public void onPostPublished(PostPublishedEvent event) {
    // Do something
  }

}

需要注意的是,只有被 @SharedEvent 注解标记的事件才能够被其他插件或者核心订阅。

发送共享事件

在插件中,我们可以通过 ApplicationEventPublisher 来发送共享事件,请看下面的示例:


@Service
public class PostService {

  private final ApplicationEventPublisher eventPublisher;

  public PostService(ApplicationEventPublisher eventPublisher) {
    this.eventPublisher = eventPublisher;
  }

  public void publishPost(Post post) {
    // Do something
    eventPublisher.publishEvent(new PostPublishedEvent(post));
  }

}

创建共享事件

在插件中,我们可以创建自定义的共享事件,供其他插件订阅,示例如下:


@SharedEvent
public class MySharedEvent extends ApplicationEvent {

  public MySharedEvent(Object source) {
    super(source);
  }

}

需要注意的是:

  1. 共享事件必须继承 ApplicationEvent
  2. 共享事件必须被 @SharedEvent 注解标记。
  3. 如果想要被其他插件订阅,则需要将该事件类发布到 Maven 仓库中,供其他插件引用。