All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.graylog2.gelfclient.transport.GelfUdpTransport Maven / Gradle / Ivy

There is a newer version: 1.5.1
Show newest version
/*
 * Copyright 2014 TORCH GmbH
 *
 * 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 org.graylog2.gelfclient.transport;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;
import io.netty.channel.socket.nio.NioDatagramChannel;
import org.graylog2.gelfclient.GelfConfiguration;
import org.graylog2.gelfclient.encoder.GelfCompressionEncoder;
import org.graylog2.gelfclient.encoder.GelfMessageChunkEncoder;
import org.graylog2.gelfclient.encoder.GelfMessageJsonEncoder;
import org.graylog2.gelfclient.encoder.GelfMessageUdpEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A {@link GelfTransport} implementation that uses UDP to send GELF messages.
 * 

This class is thread-safe.

*/ public class GelfUdpTransport extends AbstractGelfTransport { private static final Logger LOG = LoggerFactory.getLogger(GelfUdpTransport.class); /** * Creates a new UDP GELF transport. * * @param config the GELF client configuration */ public GelfUdpTransport(final GelfConfiguration config) { super(config); } @Override protected void createBootstrap(final EventLoopGroup workerGroup) { final Bootstrap bootstrap = new Bootstrap(); final GelfSenderThread senderThread = new GelfSenderThread(queue); bootstrap.group(workerGroup) .channel(NioDatagramChannel.class) .handler(new ChannelInitializer() { @Override protected void initChannel(Channel ch) throws Exception { ch.pipeline().addLast(new GelfMessageUdpEncoder(config.getRemoteAddress())); ch.pipeline().addLast(new GelfMessageChunkEncoder()); ch.pipeline().addLast(new GelfCompressionEncoder()); ch.pipeline().addLast(new GelfMessageJsonEncoder()); ch.pipeline().addLast(new SimpleChannelInboundHandler() { @Override protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception { // We do not receive data. } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { senderThread.start(ctx.channel()); } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { senderThread.stop(); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { LOG.error("Exception caught", cause); } }); } }); if (config.getSendBufferSize() != -1) { bootstrap.option(ChannelOption.SO_SNDBUF, config.getSendBufferSize()); } bootstrap.bind(0); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy