Publish events about broker's availability

Components that are using a StompBrokerRelayMessageHandler may want
to know whether or not the broker's unavailable. If they're sending
messages to the relay via an asynchronous channel there's currently
no way for them to find this out.

This commit enhances StompBrokerRelayMessageHandler to publish
application events when the broker's availability changes:
BrokerBecameAvailableEvent and BrokerBecameUnavailableEvent.
Irrespective of the number of relay sessions only a single event is
published for each change in the broker's availability.
This commit is contained in:
Andy Wilkinson
2013-08-08 16:07:01 +01:00
committed by Rossen Stoyanchev
parent be6dbe54a3
commit 8b48d8f445
7 changed files with 268 additions and 34 deletions

View File

@@ -0,0 +1,33 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.messaging.simp.stomp;
import org.springframework.context.ApplicationEvent;
/**
* Base class for application events relating to broker availability.
*
* @author Andy Wilkinson
*/
public abstract class BrokerAvailabilityEvent extends ApplicationEvent {
protected BrokerAvailabilityEvent(Object source) {
super(source);
}
}

View File

@@ -0,0 +1,38 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.messaging.simp.stomp;
/**
* Event raised when the broker being used by a {@link
* StompBrokerRelayMessageHandler} becomes available
*
* @author Andy Wilkinson
*/
public class BrokerBecameAvailableEvent extends BrokerAvailabilityEvent {
/**
* Creates a new BrokerBecameAvailableEvent
*
* @param source the {@code StompBrokerRelayMessageHandler} that is acting
* as a relay for the broker that has become available. Must not be {@code
* null}.
*/
public BrokerBecameAvailableEvent(StompBrokerRelayMessageHandler source) {
super(source);
}
}

View File

@@ -0,0 +1,39 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.messaging.simp.stomp;
/**
* Event raised when the broker being used by a {@link
* StompBrokerRelayMessageHandler} becomes unavailable
*
* @author Andy Wilkinson
*/
public class BrokerBecameUnavailableEvent extends BrokerAvailabilityEvent {
/**
* Creates a new BrokerBecameUnavailableEvent
*
* @param source the {@code StompBrokerRelayMessageHandler} that is acting
* as a relay for the broker that has become unavailable. Must not be {@code
* null}.
*/
public BrokerBecameUnavailableEvent(StompBrokerRelayMessageHandler source) {
super(source);
}
}

View File

@@ -24,9 +24,13 @@ import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.SmartLifecycle;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
@@ -51,9 +55,11 @@ import reactor.tcp.spec.TcpClientSpec;
/**
* @author Rossen Stoyanchev
* @author Andy Wilkinson
*
* @since 4.0
*/
public class StompBrokerRelayMessageHandler implements MessageHandler, SmartLifecycle {
public class StompBrokerRelayMessageHandler implements MessageHandler, SmartLifecycle, ApplicationEventPublisherAware {
private static final Log logger = LogFactory.getLog(StompBrokerRelayMessageHandler.class);
@@ -63,6 +69,8 @@ public class StompBrokerRelayMessageHandler implements MessageHandler, SmartLife
private final String[] destinationPrefixes;
private ApplicationEventPublisher applicationEventPublisher;
private String relayHost = "127.0.0.1";
private int relayPort = 61613;
@@ -83,6 +91,7 @@ public class StompBrokerRelayMessageHandler implements MessageHandler, SmartLife
private boolean running = false;
private AtomicBoolean brokerAvailable = new AtomicBoolean(false);
/**
* @param messageChannel the channel to send messages from the STOMP broker to
@@ -197,6 +206,12 @@ public class StompBrokerRelayMessageHandler implements MessageHandler, SmartLife
}
}
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher)
throws BeansException {
this.applicationEventPublisher = applicationEventPublisher;
}
/**
* Open a "system" session for sending messages from parts of the application
* not associated with a client STOMP session.
@@ -342,6 +357,18 @@ public class StompBrokerRelayMessageHandler implements MessageHandler, SmartLife
return false;
}
private void brokerAvailable() {
if (this.brokerAvailable.compareAndSet(false, true)) {
this.applicationEventPublisher.publishEvent(new BrokerBecameAvailableEvent(this));
}
}
private void brokerUnavailable() {
if (this.brokerAvailable.compareAndSet(true, false)) {
this.applicationEventPublisher.publishEvent(new BrokerBecameUnavailableEvent(this));
}
}
private class RelaySession {
@@ -356,7 +383,7 @@ public class StompBrokerRelayMessageHandler implements MessageHandler, SmartLife
private final Object monitor = new Object();
public RelaySession(String sessionId) {
private RelaySession(String sessionId) {
Assert.notNull(sessionId, "sessionId is required");
this.sessionId = sessionId;
}
@@ -404,6 +431,7 @@ public class StompBrokerRelayMessageHandler implements MessageHandler, SmartLife
if (StompCommand.CONNECTED == headers.getCommand()) {
synchronized(this.monitor) {
this.isConnected = true;
brokerAvailable();
flushMessages(this.promise.get());
}
return;
@@ -419,6 +447,8 @@ public class StompBrokerRelayMessageHandler implements MessageHandler, SmartLife
}
private void sendError(String sessionId, String errorText) {
brokerUnavailable();
StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.ERROR);
headers.setSessionId(sessionId);
headers.setMessage(errorText);