Created
June 4, 2015 05:20
-
-
Save mikaelhg/37cd661e14b8cadde799 to your computer and use it in GitHub Desktop.
RabbitMQ start/stop POC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@GrabResolver(name="OJO", root="https://oss.jfrog.org/artifactory/repo") | |
@Grab(group = "io.ratpack", module = "ratpack-groovy", version="0.9.15-SNAPSHOT") | |
@Grab(group = "org.springframework.amqp", module="spring-rabbit", version="1.4.5.RELEASE") | |
import com.rabbitmq.client.Channel | |
import groovy.transform.TupleConstructor | |
import org.springframework.amqp.core.MessageListener | |
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener | |
import org.springframework.amqp.rabbit.core.RabbitAdmin | |
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer | |
import org.springframework.amqp.rabbit.connection.AbstractConnectionFactory | |
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory | |
import org.springframework.amqp.rabbit.connection.ConnectionListener | |
import org.springframework.amqp.rabbit.connection.Connection | |
import org.springframework.amqp.core.AcknowledgeMode | |
import org.springframework.amqp.core.Message | |
import org.springframework.amqp.core.Queue | |
import org.springframework.amqp.core.DirectExchange | |
import org.springframework.amqp.core.Binding | |
import org.springframework.amqp.rabbit.core.RabbitTemplate | |
import static java.nio.charset.StandardCharsets.UTF_8 | |
import static ratpack.groovy.Groovy.ratpack | |
@TupleConstructor | |
class Listener implements ChannelAwareMessageListener, ConnectionListener { | |
private List<Message> messages | |
@Override | |
void onCreate(final Connection connection) { | |
println "ConnectionListener.onCreate: ${connection}" | |
} | |
@Override | |
void onClose(final Connection connection) { | |
println "ConnectionListener.onClose: ${connection}" | |
} | |
@Override | |
void onMessage(final Message message, final Channel channel) throws Exception { | |
println "MessageListener.onMessage: <${new String(message.body, UTF_8)}> <${channel}>" | |
messages.add(message) | |
channel.basicAck(message.messageProperties.deliveryTag, false) | |
} | |
} | |
def List<Message> messages = Collections.synchronizedList(new LinkedList<Message>()) | |
def listener = new Listener(messages: messages) | |
def connectionFactory = new CachingConnectionFactory() | |
connectionFactory.addresses = "localhost" | |
connectionFactory.requestedHeartBeat = 500 | |
connectionFactory.connectionListeners = [ listener ] | |
connectionFactory.virtualHost = "/" | |
connectionFactory.username = "guest" | |
connectionFactory.password = "guest" | |
connectionFactory.publisherConfirms = true | |
def container = new SimpleMessageListenerContainer() | |
container.connectionFactory = connectionFactory | |
container.queues = new Queue("q1") | |
container.messageListener = listener | |
container.acknowledgeMode = AcknowledgeMode.MANUAL | |
container.prefetchCount = 1 | |
ratpack { | |
handlers { | |
get { | |
response.send "Try: /messages, /start, /stop)" | |
} | |
get("messages") { | |
response.send "messages: ${messages.size()}" | |
} | |
get("start") { | |
container.start() | |
response.send "start" | |
} | |
get("stop") { | |
container.stop() | |
response.send "stop" | |
} | |
} | |
} | |
println("running...") |
docker run -it --rm -p 5672:5672 -p 15672:15672 rabbitmq:management
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://localhost:5050/
http://localhost:5050/messages
http://localhost:5050/start
http://localhost:5050/stop