camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Claus Ibsen <claus.ib...@gmail.com>
Subject Re: Camel send/receive UDP with Netty4
Date Wed, 31 Aug 2016 15:03:11 GMT
There is a fromF and toF which has built-in String.format which can
makes the DSL a bit nicer to read.

On Wed, Aug 31, 2016 at 4:32 PM, Steve Huston <shuston@riverace.com> wrote:
> I'm not an expert on the camel you show, but try changing:
> from(String.format("netty4:udp://localhost:%d?sync=false&textline=true",
>
> to
>
> from(String.format("netty4:udp://0.0.0.0:%d?sync=false&textline=true",
>
>> -----Original Message-----
>> From: Steve973 [mailto:steve973@gmail.com]
>> Sent: Wednesday, August 31, 2016 10:29 AM
>> To: users@camel.apache.org
>> Subject: Camel send/receive UDP with Netty4
>>
>> Hello, All.  I am trying to create an example where I can receive UDP packets
>> with the Netty4 component, but it isn't working.  I'm sending UDP packets
>> with a DatagramSocket to localhost, and I have a camel route consuming
>> from netty4:udp//localhost on the port the DatagramSocket sent to.  Here's
>> the code:
>>
>> public class UdpWithCamelService implements BundleActivator {
>>     private static final Logger LOGGER =
>> LoggerFactory.getLogger(UdpWithCamelService.class);
>>     public static final int SERVER_PORT = 22222;
>>     public static final int RECEIVER_PORT = 22223;
>>     private OsgiDefaultCamelContext camelContext;
>>     private RouteBuilder routeBuilder;
>>     private ScheduledExecutorService scheduler;
>>     private UdpSender sender;
>>
>>     public UdpWithCamelService() throws Exception {
>>         scheduler = Executors.newSingleThreadScheduledExecutor();
>>         sender = new UdpSender();
>>     }
>>
>>     @Override
>>     public void start(BundleContext context) throws Exception {
>>         camelContext = new OsgiDefaultCamelContext(context);
>>         camelContext.start();
>>         routeBuilder = new CamelUdpReceiverRouteBuilder();
>>         camelContext.addRoutes(routeBuilder);
>>         final Runnable senderTask = () -> {
>>             try {
>>                 sender.sendTimestamp();
>>                 LOGGER.warn("Sent timestamp via UDP");
>>             } catch (Exception e) {
>>                 LOGGER.error("Problem sending timestamp via UDP", e);
>>             }
>>         };
>>         scheduler.scheduleAtFixedRate(senderTask, 1, 1, TimeUnit.SECONDS);
>>     }
>>
>>     @Override
>>     public void stop(BundleContext context) throws Exception {
>>         scheduler.shutdown();
>>         sender.stop();
>>
>> camelContext.removeRouteDefinitions(routeBuilder.getRouteCollection().g
>> etRoutes());
>>         camelContext = null;
>>     }
>> }
>>
>> public class UdpSender {
>>     private final DatagramSocket socket;
>>
>>     public UdpSender() throws Exception {
>>         socket = new DatagramSocket(UdpWithCamelService.SERVER_PORT);
>>     }
>>
>>     public void stop() {
>>         socket.disconnect();
>>         socket.close();
>>     }
>>
>>     public void sendTimestamp() throws Exception {
>>         String timestamp = String.valueOf(System.currentTimeMillis());
>>         socket.send(
>>                 new DatagramPacket(
>>                         timestamp.getBytes(), timestamp.getBytes().length,
>> InetAddress.getLocalHost(), UdpWithCamelService.RECEIVER_PORT
>>                 )
>>         );
>>     }
>> }
>>
>> public class CamelUdpReceiverRouteBuilder extends RouteBuilder {
>>     private static final Logger LOGGER =
>> LoggerFactory.getLogger(CamelUdpReceiverRouteBuilder.class);
>>
>>     @Override
>>     public void configure() throws Exception {
>>
>> from(String.format("netty4:udp://localhost:%d?sync=false&textline=true",
>> UdpWithCamelService.RECEIVER_PORT))
>>                 .process(exchange -> {
>>                     String body = exchange.getIn().getBody(String.class);
>>                     LOGGER.warn("UDP Packet Received: " + body);
>>                 });
>>     }
>> }
>>
>> Thanks in advance,
>>
>> Steve



-- 
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2: https://www.manning.com/ibsen2

Mime
View raw message