# Java Socket

**URL:** <https://discuss.elastic.co/t/java-socket/221970>\
**Category:** APM\
**Tags:** java\
**Created:** [March 4, 2020, 2:39am UTC](https://discuss.elastic.co/t/java-socket/221970 "2020-03-04T02:39:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![hyhong](https://avatars.discourse-cdn.com/v4/letter/h/3bc359/32.png) [@hyhong](https://discuss.elastic.co/u/hyhong)\
**Post date:** [March 4, 2020, 2:39am UTC](https://discuss.elastic.co/t/java-socket/221970/1 "2020-03-04T02:39:34Z")

</div>

How to use public api with java socket?

---

<div class="post-metadata">

**Author:** ![nugusbayevkk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nugusbayevkk/32/126683_2.png) [@nugusbayevkk](https://discuss.elastic.co/u/nugusbayevkk)\
**Post date:** [March 4, 2020, 4:38am UTC](https://discuss.elastic.co/t/java-socket/221970/2 "2020-03-04T04:38:50Z")

</div>

Hello @hyhong

on server side you can do something like,

```auto
 public static void main(String[] args) throws Exception {
        try (ServerSocket listener = new ServerSocket(8086)) {
            System.out.println("The capitalization server is running...");
            ExecutorService pool = Executors.newFixedThreadPool(20);
            while (true) {
                pool.execute(new Capitalizer(listener.accept()));
            }
        }
    }

    private static class Capitalizer implements Runnable {
        private Socket socket;

        Capitalizer(Socket socket) {
            this.socket = socket;
        }

        @Override
        public void run() {
            Transaction transaction = ElasticApm.startTransaction();
            transaction.setName("CapitalizerServer#run");
            transaction.setType(Transaction.TYPE_REQUEST);
            try {
                Scanner in = new Scanner(socket.getInputStream());
                PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
                while (in.hasNextLine()) {
                    out.println(in.nextLine().toUpperCase());
                }
            } catch (Exception e) {
                transaction.captureException(e);
                System.out.println("Error:" + socket);
            } finally {
                try {
                    socket.close();
                } catch (IOException e) {
                }
                System.out.println("Closed: " + socket);
                transaction.end();
            }
        }
    }

```

---

<div class="post-metadata">

**Author:** ![hyhong](https://avatars.discourse-cdn.com/v4/letter/h/3bc359/32.png) [@hyhong](https://discuss.elastic.co/u/hyhong)\
**Post date:** [March 4, 2020, 4:51am UTC](https://discuss.elastic.co/t/java-socket/221970/3 "2020-03-04T04:51:12Z")

</div>

Thank you.But the client should have something like server side with public api.So,can former trace.

---

<div class="post-metadata">

**Author:** ![Sylvain\_Juge](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sylvain_juge/32/55521_2.png) [@Sylvain\_Juge](https://discuss.elastic.co/u/Sylvain_Juge)\
**Post date:** [March 4, 2020, 8:32am UTC](https://discuss.elastic.co/t/java-socket/221970/4 "2020-03-04T08:32:18Z")

</div>

Hi @hyhong,

Socket instrumentation can't have distributed tracing because it requires to have some way to propagate trace context and sockets do not have such feature. While it would be possible to use the socket itself to propagate context, it would require to have both ends of the socket deployed with socket instrumentation to read and write context information, and that would likely have side effects both ends of the socket.

However, what agent does is to instrument higher-level protocols like HTTP, JMS, ... those provide a way for the client and server instrumentation to propagate context through headers or metadata.

In your case, what kind of service are you accessing through raw sockets ?  
Is there any higher-level protocol (or communication library) that you could instrument here ?

---

<div class="post-metadata">

**Author:** ![hyhong](https://avatars.discourse-cdn.com/v4/letter/h/3bc359/32.png) [@hyhong](https://discuss.elastic.co/u/hyhong)\
**Post date:** [March 4, 2020, 8:41am UTC](https://discuss.elastic.co/t/java-socket/221970/5 "2020-03-04T08:41:44Z")

</div>

Thank you.We only use socket to ommunication.I have seen the git:[https://github.com/bvader/pipelineapmexample](https://github.com/bvader/pipelineapmexample).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 25, 2020, 4:41am UTC](https://discuss.elastic.co/t/java-socket/221970/6 "2020-03-25T04:41:49Z")

</div>

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.
