Created
August 17, 2015 20:54
-
-
Save dalmat36/55ec5bcfaba3d39e631e to your computer and use it in GitHub Desktop.
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
package springampqtest; | |
import org.springframework.amqp.core.BindingBuilder; | |
import org.springframework.amqp.core.Queue; | |
import org.springframework.amqp.core.TopicExchange; | |
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; | |
import org.springframework.amqp.rabbit.core.RabbitAdmin; | |
import org.springframework.amqp.rabbit.core.RabbitTemplate; | |
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer; | |
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter; | |
public class Example { | |
public static void main(String[] args) throws InterruptedException { | |
CachingConnectionFactory cf = new CachingConnectionFactory("hostName"); | |
cf.setUsername("guest"); | |
cf.setPassword("guest"); | |
// Connection connection = cf.createConnection(); | |
// set up the queue, exchange, binding on the broker | |
RabbitAdmin admin = new RabbitAdmin(cf); | |
Queue queue = new Queue("mattTestQueue2"); | |
admin.declareQueue(queue); | |
TopicExchange exchange = new TopicExchange("mattTestExchange2"); | |
admin.declareExchange(exchange); | |
admin.declareBinding( | |
BindingBuilder.bind(queue).to(exchange).with("testRoutingKey")); | |
// set up the listener and container | |
SimpleMessageListenerContainer container = | |
new SimpleMessageListenerContainer(cf); | |
Object listener = new Object() { | |
@SuppressWarnings("unused") | |
public void handleMessage(String foo) { | |
System.out.println(foo); | |
} | |
}; | |
MessageListenerAdapter adapter = new MessageListenerAdapter(listener); | |
container.setMessageListener(adapter); | |
container.setQueueNames("mattTestQueue"); | |
container.start(); | |
// send something | |
RabbitTemplate template = new RabbitTemplate(cf); | |
template.convertAndSend("mattTestExchange", "testRoutingKey", "Hello, world!"); | |
Thread.sleep(1000); | |
container.stop(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment