Skip to content

Instantly share code, notes, and snippets.

@garyrussell
Last active August 29, 2015 13:57
Show Gist options
  • Save garyrussell/9673953 to your computer and use it in GitHub Desktop.
Save garyrussell/9673953 to your computer and use it in GitHub Desktop.
/*
* Copyright 2014 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 foo;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.function.Consumer;
/**
* @author Gary Russell
*
*/
@EnableAutoConfiguration
public class RabbitConsumer {
@Bean
public RabbitPrinter printer(ConnectionFactory cf) {
return new RabbitPrinter(cf)
.forQ("fooQ")
.withConsumer(System.out::println);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(RabbitConsumer.class, args);
}
public class RabbitPrinter extends SimpleMessageListenerContainer {
public RabbitPrinter(ConnectionFactory cf) {
super(cf);
}
public RabbitPrinter forQ(String q) {
this.setQueueNames(q);
return this;
}
public RabbitPrinter withConsumer(Consumer<String> p) {
this.setMessageListener(new MessageListenerAdapter(p, "accept"));
return this;
}
}
}
/*
* Copyright 2014 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 foo;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.function.Consumer;
/**
* @author Gary Russell
*
*/
@EnableAutoConfiguration
public class RabbitConsumer2 {
public static void main(String[] args) throws Exception {
SpringApplication.run(RabbitConsumer2.class, args);
}
@Bean
public SimpleMessageListenerContainer printer(ConnectionFactory cf) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(cf);
container.setQueueNames("fooQ");
Consumer<String> p = System.out::println;
container.setMessageListener(new MessageListenerAdapter(p, "accept"));
return container;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment