47. Broadcasting Your Own Events
Bus 可以携带RemoteApplicationEvent
类型的任何 event。默认传输是 JSON,反序列化器需要知道在 time 之前将使用哪些类型。要注册新类型,必须将其放在org.springframework.cloud.bus.event
的子包中。
要自定义 event name,可以在自定义 class 上使用@JsonTypeName
,或者依赖于默认策略,即使用 class 的简单 name。
producer 和 consumer 都需要访问 class 定义。
47.1 在自定义包中注册 events
如果您不能或不想为自定义 events 使用org.springframework.cloud.bus.event
的子包,则必须使用@RemoteApplicationEventScan
annotation 指定要为RemoteApplicationEvent
类型的 events 扫描的包。使用@RemoteApplicationEventScan
指定的包包含子包。
对于 example,请考虑以下自定义 event,名为MyEvent
:
package com.acme;
public class MyEvent extends RemoteApplicationEvent {
...
}
您可以通过以下方式向反序列化器注册 event:
package com.acme;
@Configuration
@RemoteApplicationEventScan
public class BusConfiguration {
...
}
如果不指定 value,则会注册使用@RemoteApplicationEventScan
的 class 包。在此 example 中,com.acme
是使用BusConfiguration
包注册的。
您还可以使用@RemoteApplicationEventScan
上的value
,basePackages
或basePackageClasses
properties 显式指定要扫描的包,如下面的示例所示:
package com.acme;
@Configuration
//@RemoteApplicationEventScan({"com.acme", "foo.bar"})
//@RemoteApplicationEventScan(basePackages = {"com.acme", "foo.bar", "fizz.buzz"})
@RemoteApplicationEventScan(basePackageClasses = BusConfiguration.class)
public class BusConfiguration {
...
}
所有前面的@RemoteApplicationEventScan
示例都是等效的,因为通过在@RemoteApplicationEventScan
上显式指定包来注册com.acme
包。
您可以指定要扫描的多个基础包。