/** * 发送消息 * * @param queue * @param content * @author Ethan * @datetime 2015年11月9日 下午8:24:00 */ public void sendMessage(String queue, String content) { try { String routingKey = queue; // 发送消息 rabbitTemplate.setQueue(queue); rabbitTemplate.setRoutingKey(routingKey); rabbitTemplate.send(this.generateMessage(content, queue)); } catch (AmqpException e) { e.printStackTrace(); } }
@Override public void run() { try { RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory); while (true) { final Event event = events.take(); ILoggingEvent logEvent = event.getEvent(); String name = logEvent.getLoggerName(); Level level = logEvent.getLevel(); MessageProperties amqpProps = new MessageProperties(); amqpProps.setDeliveryMode(deliveryMode); amqpProps.setContentType(contentType); if (null != contentEncoding) { amqpProps.setContentEncoding(contentEncoding); } amqpProps.setHeader(CATEGORY_NAME, name); amqpProps.setHeader(CATEGORY_LEVEL, level.toString()); if (generateId) { amqpProps.setMessageId(UUID.randomUUID().toString()); } // Set timestamp Calendar tstamp = Calendar.getInstance(); tstamp.setTimeInMillis(logEvent.getTimeStamp()); amqpProps.setTimestamp(tstamp.getTime()); // Copy properties in from MDC Map<String, String> props = event.getProperties(); Set<Entry<String, String>> entrySet = props.entrySet(); for (Entry<String, String> entry : entrySet) { amqpProps.setHeader(entry.getKey(), entry.getValue()); } String[] location = locationLayout.doLayout(logEvent).split("\\|"); if (!"?".equals(location[0])) { amqpProps.setHeader( "location", String.format("%s.%s()[%s]", location[0], location[1], location[2])); } String msgBody; String routingKey = routingKeyLayout.doLayout(logEvent); // Set applicationId, if we're using one if (applicationId != null) { amqpProps.setAppId(applicationId); } if (abbreviator != null && logEvent instanceof LoggingEvent) { ((LoggingEvent) logEvent).setLoggerName(abbreviator.abbreviate(name)); msgBody = layout.doLayout(logEvent); ((LoggingEvent) logEvent).setLoggerName(name); } else { msgBody = layout.doLayout(logEvent); } // Send a message try { Message message = null; if (AmqpAppender.this.charset != null) { try { message = new Message(msgBody.getBytes(AmqpAppender.this.charset), amqpProps); } catch (UnsupportedEncodingException e) { message = new Message(msgBody.getBytes(), amqpProps); // NOSONAR (default charset) } } message = postProcessMessageBeforeSend(message, event); rabbitTemplate.send(exchangeName, routingKey, message); } catch (AmqpException e) { int retries = event.incrementRetries(); if (retries < maxSenderRetries) { // Schedule a retry based on the number of times I've tried to re-send this retryTimer.schedule( new TimerTask() { @Override public void run() { events.add(event); } }, (long) (Math.pow(retries, Math.log(retries)) * 1000)); } else { addError( "Could not send log message " + logEvent.getMessage() + " after " + maxSenderRetries + " retries", e); } } } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } }
@Override public void run() { try { RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory); while (true) { final Event event = events.take(); LoggingEvent logEvent = event.getEvent(); String name = logEvent.getLogger().getName(); Level level = logEvent.getLevel(); MessageProperties amqpProps = new MessageProperties(); amqpProps.setDeliveryMode(deliveryMode); amqpProps.setContentType(contentType); if (null != contentEncoding) { amqpProps.setContentEncoding(contentEncoding); } amqpProps.setHeader(CATEGORY_NAME, name); amqpProps.setHeader(CATEGORY_LEVEL, level.toString()); if (generateId) { amqpProps.setMessageId(UUID.randomUUID().toString()); } // Set applicationId, if we're using one if (null != applicationId) { amqpProps.setAppId(applicationId); } // Set timestamp Calendar tstamp = Calendar.getInstance(); tstamp.setTimeInMillis(logEvent.getTimeStamp()); amqpProps.setTimestamp(tstamp.getTime()); // Copy properties in from MDC @SuppressWarnings("rawtypes") Map props = event.getProperties(); @SuppressWarnings("unchecked") Set<Entry<?, ?>> entrySet = props.entrySet(); for (Entry<?, ?> entry : entrySet) { amqpProps.setHeader(entry.getKey().toString(), entry.getValue()); } LocationInfo locInfo = logEvent.getLocationInformation(); if (!"?".equals(locInfo.getClassName())) { amqpProps.setHeader( "location", String.format( "%s.%s()[%s]", locInfo.getClassName(), locInfo.getMethodName(), locInfo.getLineNumber())); } StringBuilder msgBody; String routingKey; synchronized (layoutMutex) { msgBody = new StringBuilder(layout.format(logEvent)); routingKey = routingKeyLayout.format(logEvent); } if (layout.ignoresThrowable() && null != logEvent.getThrowableInformation()) { ThrowableInformation tinfo = logEvent.getThrowableInformation(); for (String line : tinfo.getThrowableStrRep()) { msgBody.append(String.format("%s%n", line)); } } // Send a message try { Message message = null; if (AmqpAppender.this.charset != null) { try { message = new Message(msgBody.toString().getBytes(AmqpAppender.this.charset), amqpProps); } catch (UnsupportedEncodingException e) { /* fall back to default */ } } if (message == null) { message = new Message( msgBody.toString().getBytes(), amqpProps); // NOSONAR (default charset) } message = postProcessMessageBeforeSend(message, event); rabbitTemplate.send(exchangeName, routingKey, message); } catch (AmqpException e) { int retries = event.incrementRetries(); if (retries < maxSenderRetries) { // Schedule a retry based on the number of times I've tried to re-send this retryTimer.schedule( new TimerTask() { @Override public void run() { events.add(event); } }, (long) (Math.pow(retries, Math.log(retries)) * 1000)); } else { errorHandler.error( "Could not send log message " + logEvent.getRenderedMessage() + " after " + maxSenderRetries + " retries", e, ErrorCode.WRITE_FAILURE, logEvent); } } } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } }