下面列出了怎么用org.springframework.messaging.simp.annotation.SubscribeMapping的API类实例代码及写法,或者点击链接到github查看源代码。
@SubscribeMapping("/sub/{foo}/{name}")
public void subscribeEventDestinationVariable(@DestinationVariable("foo") String param1,
@DestinationVariable("name") String param2) {
this.method = "subscribeEventDestinationVariable";
this.arguments.put("foo", param1);
this.arguments.put("name", param2);
}
@SubscribeMapping("/jsonview") // not needed for the tests but here for completeness
@JsonView(MyJacksonView1.class)
public JacksonViewBean getJsonView() {
JacksonViewBean payload = new JacksonViewBean();
payload.setWithView1("with");
payload.setWithView2("with");
payload.setWithoutView("without");
return payload;
}
@SubscribeMapping("/sub/{foo}/{name}")
public void subscribeEventDestinationVariable(@DestinationVariable("foo") String param1,
@DestinationVariable("name") String param2) {
this.method = "subscribeEventDestinationVariable";
this.arguments.put("foo", param1);
this.arguments.put("name", param2);
}
@SubscribeMapping("/jsonview") // not needed for the tests but here for completeness
@JsonView(MyJacksonView1.class)
public JacksonViewBean getJsonView() {
JacksonViewBean payload = new JacksonViewBean();
payload.setWithView1("with");
payload.setWithView2("with");
payload.setWithoutView("without");
return payload;
}
/**
* This mapping is bypassing the spring WebSocket broker (because of SubscribeMapping) and send the response
* directly to the client who subscribed, and only him.
* <b>READ CAREFULLY</b>: since the response is send ignoring the message broker, client will have to provide the
* application destination prefix (/joal/events/replay)
*
* @return an ordered list of all needed event to rebuild the current application state
*/
@SubscribeMapping("/initialize-me")
public List<StompMessage> list() {
final LinkedList<StompMessage> events = new LinkedList<>();
// client files list
events.add(StompMessage.wrap(new ListOfClientFilesPayload(new ListOfClientFilesEvent(this.seedManager.listClientFiles()))));
// config
events.addFirst(StompMessage.wrap(new ConfigHasBeenLoadedPayload(new ConfigHasBeenLoadedEvent(this.seedManager.getCurrentConfig()))));
// torrent files list
for (final MockedTorrent torrent : this.seedManager.getTorrentFiles()) {
events.addFirst(StompMessage.wrap(new TorrentFileAddedPayload(new TorrentFileAddedEvent(torrent))));
}
// global state
if (this.seedManager.isSeeding()) {
events.addFirst(StompMessage.wrap(new GlobalSeedStartedPayload(this.seedManager.getCurrentEmulatedClient())));
} else {
events.addFirst(StompMessage.wrap(new GlobalSeedStoppedPayload()));
}
// speeds
final Map<InfoHash, Speed> speedMap = this.seedManager.getSpeedMap();
if (!speedMap.isEmpty()) {
events.addFirst(StompMessage.wrap(new SeedingSpeedHasChangedPayload(new SeedingSpeedsHasChangedEvent(speedMap))));
}
// Announcers are the most likely to change due to a concurrent access, so we gather them as late as possible, and we put them at the top of the list.
for (final AnnouncerFacade announcerFacade : this.seedManager.getCurrentlySeedingAnnouncer()) {
events.addFirst(StompMessage.wrap(new SuccessfullyAnnouncePayload(new SuccessfullyAnnounceEvent(announcerFacade, RequestEvent.STARTED))));
}
return events;
}
@SubscribeMapping("/sub/{foo}/{name}")
public void subscribeEventDestinationVariable(@DestinationVariable("foo") String param1,
@DestinationVariable("name") String param2) {
this.method = "subscribeEventDestinationVariable";
this.arguments.put("foo", param1);
this.arguments.put("name", param2);
}
@SubscribeMapping("/jsonview") // not needed for the tests but here for completeness
@JsonView(MyJacksonView1.class)
public JacksonViewBean getJsonView() {
JacksonViewBean payload = new JacksonViewBean();
payload.setWithView1("with");
payload.setWithView2("with");
payload.setWithoutView("without");
return payload;
}
@SubscribeMapping("/topic/activity")
@SendTo("/topic/tracker")
public ActivityDTO sendActivity(@Payload ActivityDTO activityDTO, StompHeaderAccessor stompHeaderAccessor, Principal principal) {
activityDTO.setUserLogin(SecurityUtils.getCurrentUserLogin());
activityDTO.setUserLogin(principal.getName());
activityDTO.setSessionId(stompHeaderAccessor.getSessionId());
activityDTO.setIpAddress(stompHeaderAccessor.getSessionAttributes().get(IP_ADDRESS).toString());
Instant instant = Instant.ofEpochMilli(Calendar.getInstance().getTimeInMillis());
activityDTO.setTime(dateTimeFormatter.format(ZonedDateTime.ofInstant(instant, ZoneOffset.systemDefault())));
log.debug("Sending user tracking data {}", activityDTO);
return activityDTO;
}
@SubscribeMapping("/topic/activity")
@SendTo("/topic/tracker")
public ActivityDTO sendActivity(@Payload ActivityDTO activityDTO, StompHeaderAccessor stompHeaderAccessor, Principal principal) {
activityDTO.setUserLogin(SecurityUtils.getCurrentUserLogin());
activityDTO.setUserLogin(principal.getName());
activityDTO.setSessionId(stompHeaderAccessor.getSessionId());
activityDTO.setIpAddress(stompHeaderAccessor.getSessionAttributes().get(IP_ADDRESS).toString());
Instant instant = Instant.ofEpochMilli(Calendar.getInstance().getTimeInMillis());
activityDTO.setTime(dateTimeFormatter.format(ZonedDateTime.ofInstant(instant, ZoneOffset.systemDefault())));
log.debug("Sending user tracking data {}", activityDTO);
return activityDTO;
}
@SubscribeMapping("/topic/activity")
@SendTo("/topic/tracker")
public ActivityDTO sendActivity(@Payload ActivityDTO activityDTO, StompHeaderAccessor stompHeaderAccessor, Principal principal) {
activityDTO.setUserLogin(SecurityUtils.getCurrentLogin());
activityDTO.setUserLogin(principal.getName());
activityDTO.setSessionId(stompHeaderAccessor.getSessionId());
activityDTO.setIpAddress(stompHeaderAccessor.getSessionAttributes().get(IP_ADDRESS).toString());
activityDTO.setTime(dateTimeFormatter.print(Calendar.getInstance().getTimeInMillis()));
log.debug("Sending user tracking data {}", activityDTO);
return activityDTO;
}
@SubscribeMapping("/user/queue/pocketcards")
public List<PocketCardsDTO> fetchPocketCards(Principal principal) {
var playerId = loginRepository.fetchAggregateIdByUsername(principal.getName());
var pocketCardsForUser = cardsUsedInHandRepository.fetchAllPocketCardsForUser(playerId);
return pocketCardsForUser.entrySet().stream()
.map(x -> new PocketCardsDTO(x.getKey(), x.getValue().getCard1().getId(), x.getValue().getCard2().getId()))
.collect(Collectors.toList());
}
@SubscribeMapping("/get")
public PlayQueueInfo getPlayQueue(@DestinationVariable int playerId, SimpMessageHeaderAccessor headers) throws Exception {
Player player = getPlayer(playerId, headers);
return playQueueService.getPlayQueueInfo(player);
}
@SubscribeMapping("/readable")
public List<Playlist> getReadablePlaylists(Principal p) {
return playlistService.getReadablePlaylistsForUser(p.getName());
}
@SubscribeMapping("/{id}")
public Playlist getPlaylist(Principal p, @DestinationVariable int id) {
return new PlaylistService.BroadcastedPlaylist(playlistService.getPlaylist(id), true);
}
@SubscribeMapping("/nowPlaying/current")
public List<NowPlayingInfo> getActivePlays() {
return statusService.getActivePlays();
}
@SubscribeMapping("/nowPlaying/recent")
public List<NowPlayingInfo> getInactivePlays() {
return statusService.getInactivePlays();
}
@Override
public boolean supportsReturnType(MethodParameter returnType) {
return (returnType.hasMethodAnnotation(SubscribeMapping.class) &&
!returnType.hasMethodAnnotation(SendTo.class) &&
!returnType.hasMethodAnnotation(SendToUser.class));
}
@SubscribeMapping("/data") // not needed for the tests but here for completeness
private String getData() {
return PAYLOAD;
}
@SubscribeMapping("/data") // not needed for the tests but here for completeness
@SendTo("/sendToDest")
private String getDataAndSendTo() {
return PAYLOAD;
}
@SubscribeMapping("/foo")
public String handleSubscribe() {
return "bar";
}
@SubscribeMapping("/foo")
public String handleSubscribe() {
return "bar";
}
@SubscribeMapping("/number")
public int number() {
return 42;
}
@Override
public boolean supportsReturnType(MethodParameter returnType) {
return (returnType.hasMethodAnnotation(SubscribeMapping.class) &&
!returnType.hasMethodAnnotation(SendTo.class) &&
!returnType.hasMethodAnnotation(SendToUser.class));
}
@SubscribeMapping("/data") // not needed for the tests but here for completeness
private String getData() {
return PAYLOAD;
}
@SubscribeMapping("/data") // not needed for the tests but here for completeness
@SendTo("/sendToDest")
private String getDataAndSendTo() {
return PAYLOAD;
}
@SubscribeMapping("/foo")
public String handleSubscribe() {
return "bar";
}
@SubscribeMapping("/foo")
public String handleSubscribe() {
return "bar";
}
@SubscribeMapping("/number")
public int number() {
return 42;
}
@Override
public boolean supportsReturnType(MethodParameter returnType) {
return (returnType.getMethodAnnotation(SubscribeMapping.class) != null &&
returnType.getMethodAnnotation(SendTo.class) == null &&
returnType.getMethodAnnotation(SendToUser.class) == null);
}
@SubscribeMapping("/data") // not needed for the tests but here for completeness
private String getData() {
return PAYLOAD;
}