1

I have to create tcp server which reponse to request to the client and also send the event to the client at fixed frequency.Tcp server code:

from("netty:tcp://localhost:7100?sync=true&allowDefaultCodec=false&encoder=#stringEncode&decoder=#stringDecode")
                .to("bean:echoService");

EchoService bean code is called when my server has to send response to msg from client

@Service
    public class EchoService {
      public String sayHello(Object guestName) {
        System.out.println("Input guestName : "+ guestName);
        return "Hello " + guestName;
      }
    }

And I have another MessageService bean which send the Hi message at fixed frequency.I want to integrate this bean to my server so that I can send "Hi" msg to client

 @Service
        public class MessageService {
          public String sayHi() {
            System.out.println("sending hi : ");
            return "Hi ";
          }
        }

I am not able to decide how to integrate MessageService bean "hi" msg with tcp server to send TCP client.PS:- I am new to camel.

  • You wish to send the events to client. How is the client consuming the events? Is it listening to some tcp port, jms queue or something. Also what invokes your MessageService, you say it sends 'Hi' at a fixed frequency. – Gautam Aug 29 '17 at 06:45
  • There is no sich thing as 'send event to client'. All you can send to the client is *data*. Unclear what you're asking. – user207421 Aug 29 '17 at 09:31
  • @Gautam .Client is listening to port 7100 tcp connection.And I am using Spring scheduler to send message to client at frequency – UchihaObito Sep 05 '17 at 03:57
  • @EJP Here event means server will send message to client at 7100 port which client will consume only.And this will be trigger on the basis of spring scheduler which will send the message to client on fixed frequency – UchihaObito Sep 05 '17 at 04:02
  • @UchihaObito So from what I understand here, is that `MessageService` is the client actually. Your camel module is actually a server listening to port 7100. Camel module has a route listening to port 7100, netty component, and you wish to send that to `EchoService`. Then in your `MessageService` you need to implement a tcp client. Just write a `Socket` and send data to it. That will trigger your Camel route. – Gautam Sep 05 '17 at 04:30
  • @Gautam I want how to integrate the MessageService with tcp server code to send message to tcp client.EchoService is called when client send message and my server has to send response to it. MessageService is the message which my server send after fixed interval of time to client.I want to know how to integrate MessageService message. – UchihaObito Sep 05 '17 at 09:57
  • 'Server will send message to client at 7100 port which client will consume only': I don't know what 'only' means in this sentence, or what is actually stopping you from accomplishing this task, or what port 7100 has to do with it. If the client exists, you already have a *connection* to him, so all you need to do is send the data over *that* connection. – user207421 Sep 05 '17 at 10:07
  • @UchihaObito, so help me understand first. You mean above Camel route is a synchronous route where client makes a call to tcp port 7100 and that route sends it to `EchoService`. And that the route is working just fine, as expected with `EchoService` responding to the client. Now you wish to send messages to a tcp client, at regular intervals, as that is what you are expecting `MessageService` to do. How will you even determine what all clients to send messages to? – Gautam Sep 05 '17 at 11:39
  • @gautam ur right . Any client which is hearing on that port will receive the "Hi" message. – UchihaObito Sep 05 '17 at 16:08
  • So your clients are really servers? Very hard to understand all this. – user207421 Sep 06 '17 at 00:03
  • @UchihaObito, how will you determine which client to send message(s) to? Will there be just one "client" out there? Then you can write another Camel route that starts with `timer` component to `bean` `MessageService` to `tcp` endpoint of the client. As @EJP said, your client is actually a server in this case. Also your first route is not really relevant here, is it? – Gautam Sep 06 '17 at 03:39
  • @gautam yes my client is acting like server and server is acting like client in case of "hi" message only .But sometime guestName comes from the client then above code will be needed for server.There is only one client listening to 7100. – UchihaObito Sep 06 '17 at 07:07
  • @EJP yes my server acts as client when it receives the guestName and it also acts as server when it generates "hi" message. – UchihaObito Sep 07 '17 at 01:25
  • @UchihaObito, so as mentioned earlier, just add a new route using `timer` component which invokes `MessageService` `bean` component and then uses `tcp` component to send the data to the client. – Gautam Sep 07 '17 at 06:35

0 Answers0