mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
update bus example
This commit is contained in:
parent
1b9cbf7396
commit
69f6ad9128
@ -16,8 +16,6 @@
|
|||||||
*/
|
*/
|
||||||
package org.springframework.cloud.alibaba.cloud.examples.rocketmq;
|
package org.springframework.cloud.alibaba.cloud.examples.rocketmq;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
@ -30,6 +28,9 @@ import org.springframework.web.bind.annotation.GetMapping;
|
|||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* RocketMQ Bus Spring Application
|
* RocketMQ Bus Spring Application
|
||||||
*
|
*
|
||||||
@ -44,7 +45,9 @@ public class RocketMQBusApplication {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
new SpringApplicationBuilder(RocketMQBusApplication.class)
|
new SpringApplicationBuilder(RocketMQBusApplication.class)
|
||||||
.properties("server.port=0") // Random server port
|
.properties("server.port=0") // Random server port
|
||||||
.properties("management.endpoints.web.exposure.include=*") // exposure includes all
|
.properties("management.endpoints.web.exposure.include=*") // exposure
|
||||||
|
// includes
|
||||||
|
// all
|
||||||
.properties("spring.cloud.bus.trace.enabled=true") // Enable trace
|
.properties("spring.cloud.bus.trace.enabled=true") // Enable trace
|
||||||
.run(args);
|
.run(args);
|
||||||
}
|
}
|
||||||
@ -69,11 +72,13 @@ public class RocketMQBusApplication {
|
|||||||
* @return If published
|
* @return If published
|
||||||
*/
|
*/
|
||||||
@GetMapping("/bus/event/publish/user")
|
@GetMapping("/bus/event/publish/user")
|
||||||
public boolean publish(@RequestParam String name, @RequestParam(required = false) String destination) {
|
public boolean publish(@RequestParam String name,
|
||||||
|
@RequestParam(required = false) String destination) {
|
||||||
User user = new User();
|
User user = new User();
|
||||||
user.setId(System.currentTimeMillis());
|
user.setId(System.currentTimeMillis());
|
||||||
user.setName(name);
|
user.setName(name);
|
||||||
publisher.publishEvent(new UserRemoteApplicationEvent(user, originService, destination));
|
publisher.publishEvent(
|
||||||
|
new UserRemoteApplicationEvent(this, user, originService, destination));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,11 +89,14 @@ public class RocketMQBusApplication {
|
|||||||
*/
|
*/
|
||||||
@EventListener
|
@EventListener
|
||||||
public void onEvent(UserRemoteApplicationEvent event) {
|
public void onEvent(UserRemoteApplicationEvent event) {
|
||||||
System.out.printf("Server [port : %d] listeners on %s\n", localServerPort, event.getUser());
|
System.out.printf("Server [port : %d] listeners on %s\n", localServerPort,
|
||||||
|
event.getUser());
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventListener
|
@EventListener
|
||||||
public void onAckEvent(AckRemoteApplicationEvent event) throws JsonProcessingException {
|
public void onAckEvent(AckRemoteApplicationEvent event)
|
||||||
System.out.printf("Server [port : %d] listeners on %s\n", localServerPort, objectMapper.writeValueAsString(event));
|
throws JsonProcessingException {
|
||||||
|
System.out.printf("Server [port : %d] listeners on %s\n", localServerPort,
|
||||||
|
objectMapper.writeValueAsString(event));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,12 +26,22 @@ import org.springframework.cloud.bus.event.RemoteApplicationEvent;
|
|||||||
*/
|
*/
|
||||||
public class UserRemoteApplicationEvent extends RemoteApplicationEvent {
|
public class UserRemoteApplicationEvent extends RemoteApplicationEvent {
|
||||||
|
|
||||||
public UserRemoteApplicationEvent(User user, String originService,
|
private User user;
|
||||||
|
|
||||||
|
public UserRemoteApplicationEvent() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserRemoteApplicationEvent(Object source, User user, String originService,
|
||||||
String destinationService) {
|
String destinationService) {
|
||||||
super(user, originService, destinationService);
|
super(source, originService, destinationService);
|
||||||
|
this.user = user;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUser(User user) {
|
||||||
|
this.user = user;
|
||||||
}
|
}
|
||||||
|
|
||||||
public User getUser() {
|
public User getUser() {
|
||||||
return (User) getSource();
|
return user;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user