Java tutorial
/** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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 ylj.demo.mq.amqp.rabbitmq_client; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; import com.rabbitmq.client.Consumer; import com.rabbitmq.client.Envelope; import com.rabbitmq.client.ExceptionHandler; import com.rabbitmq.client.Recoverable; import com.rabbitmq.client.RecoveryListener; import com.rabbitmq.client.ShutdownSignalException; import com.rabbitmq.client.TopologyRecoveryException; import com.rabbitmq.client.impl.recovery.AutorecoveringConnection; import java.io.IOException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import org.apache.log4j.xml.DOMConfigurator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.rabbitmq.client.AMQP.BasicProperties; import com.rabbitmq.client.Channel; class DemoConsumer { final static Logger logger = LoggerFactory.getLogger(DemoConsumer.class); public static final String UTF_8 = "UTF-8"; public static class MyExceptionHandler implements ExceptionHandler { @Override public void handleUnexpectedConnectionDriverException(Connection conn, Throwable exception) { // TODO Auto-generated method stub } @Override public void handleReturnListenerException(Channel channel, Throwable exception) { // TODO Auto-generated method stub } @Override public void handleFlowListenerException(Channel channel, Throwable exception) { // TODO Auto-generated method stub } @Override public void handleConfirmListenerException(Channel channel, Throwable exception) { // TODO Auto-generated method stub } @Override public void handleBlockedListenerException(Connection connection, Throwable exception) { // TODO Auto-generated method stub } @Override public void handleConsumerException(Channel channel, Throwable exception, Consumer consumer, String consumerTag, String methodName) { // TODO Auto-generated method stub } @Override public void handleConnectionRecoveryException(Connection conn, Throwable exception) { // TODO Auto-generated method stub } @Override public void handleChannelRecoveryException(Channel ch, Throwable exception) { // TODO Auto-generated method stub } @Override public void handleTopologyRecoveryException(Connection conn, Channel ch, TopologyRecoveryException exception) { // TODO Auto-generated method stub } } public static class MyConsumer implements Consumer { @Override public void handleConsumeOk(String consumerTag) { // TODO Auto-generated method stub } @Override public void handleCancelOk(String consumerTag) { // TODO Auto-generated method stub } @Override public void handleCancel(String consumerTag) throws IOException { // TODO Auto-generated method stub } @Override public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body) throws IOException { String routingKey = envelope.getRoutingKey(); long deliveryTag = envelope.getDeliveryTag(); String contentType = properties.getContentType(); try { String message = new String(body, UTF_8); logger.info("consumerTag:" + consumerTag + " routingKey:" + routingKey + " deliveryTag:" + deliveryTag + " contentType:" + contentType + " " + message); } catch (Exception e) { e.printStackTrace(); } } @Override public void handleShutdownSignal(String consumerTag, ShutdownSignalException sig) { // TODO Auto-generated method stub } @Override public void handleRecoverOk(String consumerTag) { // TODO Auto-generated method stub } } public static class MyRecoveryListener implements RecoveryListener { @Override public void handleRecovery(Recoverable recoverable) { logger.info("handleRecovery recoverable" + recoverable); } } public static void doConsume() throws Exception { ExecutorService consumeES = Executors.newFixedThreadPool(2); ConnectionFactory factory = new ConnectionFactory(); factory.setUri("amqp://guest:guest@newsrec10.photo.163.org:5672"); // factory.setUri("amqp://guest:guest@/?brokerlist='tcp://newsrec10.photo.163.org:5672'"); factory.setAutomaticRecoveryEnabled(true); factory.setNetworkRecoveryInterval(10000); factory.setExceptionHandler(new MyExceptionHandler()); AutorecoveringConnection conn = (AutorecoveringConnection) factory.newConnection(consumeES); conn.addRecoveryListener(new MyRecoveryListener()); // ?queue?? Channel channel = conn.createChannel(); // channel.close(); // channel.queueDeclare("recsys.news.userfeed.usermodel", false, false, false, null); // channel.basicConsume("recsys.news.userfeed.usermodel", true, null, new MyConsumer()); channel.basicConsume("recsys.news.userfeed.usermodel", new MyConsumer()); // conn.close(); /* * work??work???? channel.basicConsume("hello",false,consumer); channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); * */ } public static void main(String[] args) throws Exception { DOMConfigurator.configure("conf/log4j.xml"); //Queuemessage doConsume(); } }