List of usage examples for com.rabbitmq.client ReturnListener ReturnListener
ReturnListener
From source file:biospectra.classify.server.RabbitMQInputServer.java
License:Apache License
public synchronized void connect() throws IOException, TimeoutException { LOG.info("Connecting to - " + this.conf.getRabbitMQHostname() + ":" + this.conf.getRabbitMQPort()); ConnectionFactory factory = new ConnectionFactory(); factory.setHost(this.conf.getRabbitMQHostname()); factory.setPort(this.conf.getRabbitMQPort()); factory.setUsername(this.conf.getRabbitMQUserId()); factory.setPassword(this.conf.getRabbitMQUserPwd()); factory.setRequestedHeartbeat(60);// w ww .j av a 2s. co m factory.setAutomaticRecoveryEnabled(true); this.connection = factory.newConnection(); this.connection.addShutdownListener(new ShutdownListener() { @Override public void shutdownCompleted(ShutdownSignalException sse) { LOG.error("connection shutdown", sse); } }); this.requestChannel = this.connection.createChannel(); this.responseChannel = this.connection.createChannel(); LOG.info("connected."); this.requestChannel.basicQos(1); this.requestChannel.queueDeclare("request", false, false, true, null); this.responseChannel.addReturnListener(new ReturnListener() { @Override public void handleReturn(int replyCode, String replyText, String exchange, String routingKey, AMQP.BasicProperties properties, byte[] body) throws IOException { String message = new String(body, "UTF-8"); LOG.info("message not delivered to " + routingKey); LOG.info(message); } }); this.consumer = new DefaultConsumer(this.requestChannel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String message = new String(body, "UTF-8"); this.getChannel().basicAck(envelope.getDeliveryTag(), false); if (handler != null) { ClassificationRequestMessage req = ClassificationRequestMessage.createInstance(message); handler.handleMessage(req, properties.getReplyTo()); } } }; this.workerThread = new Thread(new Runnable() { @Override public void run() { try { requestChannel.basicConsume("request", consumer); LOG.info("Waiting for messages"); } catch (IOException ex) { LOG.error("Exception occurred while consuming a message", ex); } } }); this.workerThread.start(); }
From source file:com.streamsets.pipeline.stage.destination.rabbitmq.RabbitTarget.java
License:Apache License
@Override public List<ConfigIssue> init() { List<ConfigIssue> issues = super.init(); //Validate AMQP Properties only if it is set. if (conf.basicPropertiesConfig.setAMQPMessageProperties) { RabbitUtil.buildBasicProperties(conf.basicPropertiesConfig, getContext(), builder, issues); }//from ww w .j a v a 2 s. co m //Initialize Rabbit Channel, Queue And Exchange //Also initialize dataFormatConfig RabbitUtil.initRabbitStage(getContext(), conf, conf.dataFormat, conf.dataFormatConfig, rabbitCxnManager, issues); if (issues.isEmpty()) { //Set a return listener for mandatory flag failure. this.rabbitCxnManager.getChannel().addReturnListener(new ReturnListener() { @Override public void handleReturn(int replyCode, String replyText, String exchange, String routingKey, AMQP.BasicProperties properties, byte[] body) throws IOException { LOG.error(Errors.RABBITMQ_08.getMessage(), replyCode, replyText, exchange, routingKey); getContext().reportError(Errors.RABBITMQ_08, replyCode, replyText, exchange, routingKey); } }); generatorFactory = conf.dataFormatConfig.getDataGeneratorFactory(); errorRecordHandler = new DefaultErrorRecordHandler(getContext()); } return issues; }
From source file:net.roboconf.messaging.internal.client.rabbitmq.DmClient.java
License:Apache License
@Override public void openConnection(AbstractMessageProcessor messageProcessor) throws IOException { // Already connected? Do nothing this.logger.fine("The DM is opening a connection to RabbitMQ."); if (isConnected()) { this.logger.info("The DM has already a connection to RabbitMQ."); return;//w ww. jav a 2 s .c o m } // Initialize the connection ConnectionFactory factory = new ConnectionFactory(); RabbitMqUtils.configureFactory(factory, this.messageServerIp, this.username, this.password); this.channel = factory.newConnection().createChannel(); // Be notified when a message does not arrive in a queue (i.e. nobody is listening) this.channel.addReturnListener(new ReturnListener() { @Override public void handleReturn(int replyCode, String replyText, String exchange, String routingKey, BasicProperties properties, byte[] body) throws IOException { String messageType = "undetermined"; try { Message msg = SerializationUtils.deserializeObject(body); messageType = msg.getClass().getName(); } catch (ClassNotFoundException e) { DmClient.this.logger.severe("Failed to deserialize a message object."); DmClient.this.logger.finest(Utils.writeException(e)); } StringBuilder sb = new StringBuilder(); sb.append("A message sent by the DM was not received by any agent queue."); sb.append("\nMessage type: " + messageType); sb.append("\nRouting key: " + routingKey); sb.append("\nReason: " + replyText); DmClient.this.logger.warning(sb.toString()); } }); // Store the message processor for later this.messageProcessor = messageProcessor; this.messageProcessor.start(); }
From source file:rapture.exchange.rabbitmq.RabbitExchangeHandler.java
License:Open Source License
@Override public synchronized void setConfig(Map<String, String> config) { // Attempt to bind to RabbitMQ ConnectionFactory factory = new ConnectionFactory(); logger.info(Messages.getString("RabbitExchangeHandler.config")); //$NON-NLS-1$ try {/*from w w w .j a v a2 s . com*/ String uri = MultiValueConfigLoader.getConfig("RABBITMQ-" //$NON-NLS-1$ + instanceName); if (uri == null || uri.isEmpty()) { uri = "amqp://guest:guest@localhost:5672/%2f"; //$NON-NLS-1$ } factory.setUri(uri); factory.setAutomaticRecoveryEnabled(true); logger.debug(Messages.getString("RabbitExchangeHandler.creatingChannel")); //$NON-NLS-1$ connection = factory.newConnection(); connection.addShutdownListener(new ShutdownListener() { @Override public void shutdownCompleted(ShutdownSignalException cause) { // This should theoretically be called when we disconnect // from RabbitMQ, but due to a bug in the client library it // instead gets invoked // when reconnecting. This may change in future versions of // amqp-client so may need to revisit logger.info("Reconnected to RabbitMQ"); } }); logger.debug(Messages.getString("RabbitExchangeHandler.connectionMade")); //$NON-NLS-1$ channel = connection.createChannel(); channel.addShutdownListener(new ShutdownListener() { @Override public void shutdownCompleted(ShutdownSignalException cause) { logger.info("Disconnected from RabbitMQ. Cause :" + cause.getMessage()); logger.debug(ExceptionToString.format(cause)); } }); logger.debug(Messages.getString("RabbitExchangeHandler.channelCreated")); //$NON-NLS-1$ channel.basicQos(100); channel.addReturnListener(new ReturnListener() { @Override public void handleReturn(int replyCode, String replyText, String exchange, String routingKey, AMQP.BasicProperties properties, byte[] body) throws IOException { logger.debug( String.format(Messages.getString("RabbitExchangeHandler.returnListener"), replyCode, //$NON-NLS-1$ replyText)); } }); channel.addFlowListener(new FlowListener() { @Override public void handleFlow(boolean active) throws IOException { logger.debug(String.format(Messages.getString("RabbitExchangeHandler.Flow"), active)); //$NON-NLS-1$ } }); replyQueueName = channel.queueDeclare().getQueue(); logger.info("RPC reply queue is " + replyQueueName); consumer = new QueueingConsumer(channel); channel.basicConsume(replyQueueName, true, consumer); } catch (Exception e) { String message = Messages.getString("RabbitExchangeHandler.noConnect"); throw RaptureExceptionFactory.create(HttpURLConnection.HTTP_INTERNAL_ERROR, message, e); } }