-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmore.html
800 lines (621 loc) · 59.3 KB
/
more.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<meta content="True" name="HandheldFriendly">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<meta name="viewport" content="width=device-width">
<meta name="description" content="nginx-clojure.github.io : Nginx module for embedding Clojure / Java / Groovy programs, typically those Ring based handlers">
<link rel="stylesheet" type="text/css" media="screen" href="stylesheets/stylesheet.css">
<!-- bootstrap cdn -->
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-theme.min.css">
<script src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.0.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<script src="javascripts/fix.js"></script>
<link rel="stylesheet" type="text/css" media="screen" href="stylesheets/my.css">
<title>More@Nginx-Clojure</title>
</head>
<body>
<div id="header">
<!-- HEADER -->
<nav id="header_wrap" class="" role="navigation">
<header id="header_logo_title" class="inner container">
<ul class="external-link-buttons">
<li>
<a id="forkme_banner" href="https://github.com/nginx-clojure/nginx-clojure" target="_blank">View on GitHub</a>
</li>
<!-- <li>
<a id="google_group_banner" href="https://groups.google.com/forum/#!forum/nginx-clojure" target="_blank">Google Group</a>
</li> -->
</ul>
<div id="project_logo">
<h1 id="project_title">nginx-clojure</h1>
<ul class="social-buttons">
<li>
<iframe class="github-btn" src="github-btn.html?user=nginx-clojure&repo=nginx-clojure&type=watch&count=true" allowtransparency="true" frameborder="0" scrolling="0" width="102px" height="20px"></iframe>
</li>
<li>
<iframe class="github-btn" src="github-btn.html?user=nginx-clojure&repo=nginx-clojure&type=fork&count=true" allowtransparency="true" frameborder="0" scrolling="0" width="100px" height="20px"></iframe>
</li>
</ul></div>
<h6 id="project_tagline">Nginx module for embedding Clojure / Java / Groovy programs, typically those Ring based handlers</h6>
</header>
<div class="btn-group" style="border-top-width: 1px;border-top-color: #fff;">
<a href="index.html" class="btn btn-sample btn-md"><span class="glyphicon glyphicon-home"></span> Home</a>
<a href="quickstart.html" class="btn btn-sample btn-md"><span class="glyphicon glyphicon-flash"></span> Quick Start</a>
<a href="downloads.html" class="btn btn-sample btn-md"><span class="glyphicon glyphicon-download-alt"></span> Downloads</a>
<a href="installation.html" class="btn btn-sample btn-md"><span class="glyphicon glyphicon-hdd"></span> Installation</a>
<div class="btn-group">
<button type="button" class="btn btn-sample btn-md dropdown-toggle" data-toggle="dropdown" id="docDropDownBtn"><span class="glyphicon glyphicon-cog"></span> Configuration <span class="caret"></span></button>
<ul class="dropdown-menu" role="menu">
<li><a href="configuration.html#user-content-21-jvm-path--class-path--other-jvm-options"><span class="glyphicon glyphicon-cog"></span> JVM Path,Class Path & Other JVM Options</a></li>
<li><a href="configuration.html#user-content-22-initialization-handler-for-nginx-worker"><span class="glyphicon glyphicon-cog"></span> Initialization Handler for nginx worker</a></li>
<li><a href="configuration.html#user-content-23-content-ring-handler-for-location"><span class="glyphicon glyphicon-cog"></span> Content Ring Handler for Location</a></li>
<li><a href="configuration.html#user-content-24-chose--coroutine-based-socket-or-asynchronous-socketchannel-or-thread-pool-for-slow-io-operations"><span class="glyphicon glyphicon-cog"></span> Coroutine/Asynchronous Client Channel/Thread Pool </a></li>
<li><a href="configuration.html#user-content-25-nginx-rewrite-handler"><span class="glyphicon glyphicon-cog"></span> Nginx Rewrite Handler</a></li>
<li><a href="configuration.html#user-content-26-nginx-access-handler"><span class="glyphicon glyphicon-cog"></span> Nginx Access Handler</a></li>
<li><a href="configuration.html#user-content-27-nginx-header-filter"><span class="glyphicon glyphicon-cog"></span> Nginx Header Filter</a></li>
<li><a href="configuration.html#user-content-28-nginx-body-filter"><span class="glyphicon glyphicon-cog"></span> Nginx Body Filter</a></li>
<li><a href="configuration.html#user-content-29-nginx-log-handler"><span class="glyphicon glyphicon-cog"></span> Nginx Log Handler</a></li>
</ul>
</div>
<div class="btn-group">
<button type="button" class="btn btn-sample btn-md dropdown-toggle" data-toggle="dropdown" id="docDropDownBtn"><span class="glyphicon glyphicon-book"></span> Documents <span class="caret"></span></button>
<ul class="dropdown-menu" role="menu">
<!-- <li><a href="quickstart.html"><span class="glyphicon glyphicon-flash"></span> Quick Start</a></li> -->
<li><a href="directives.html"><span class="glyphicon glyphicon-th-list text-warning"></span> Directives Reference</a></li>
<li><a href="embed.html"><span class="glyphicon glyphicon-gift"></span> Embedding Nginx-Clojure into A standard App</a></li>
<li><a href="more.html#user-content-34-server-channel-for-long-polling--server-sent-events-sse"><span class="glyphicon glyphicon-envelope"></span> Server Channel for Long Polling & Server Sent Events</a></li>
<li><a href="subpub.html"><span class="glyphicon glyphicon-envelope text-warning"></span> Pub/Sub Among Nginx Worker Processes</a></li>
<li><a href="sharedmap.html"><span class="glyphicon glyphicon-envelope text-warning"></span> Shared Map & Session Store</a></li>
<li><a href="more.html#user-content-36-asynchronous-client-channel"><span class="glyphicon glyphicon-road"></span> Asynchronous Client Channel</a></li>
<li><a href="more.html#user-content-37--about-logging"><span class="glyphicon glyphicon-th-list"></span> About Logging</a></li>
<li><a href="more.html#user-content-38--sever-side-websocket"><span class="glyphicon glyphicon-th-list"></span> Sever Side WebSocket</a></li>
<li><a href="more.html#user-content-39--java-standard-restful-web-services-with-jersey"><span class="glyphicon glyphicon-th-list"></span> Java standard RESTful web services with Jersey</a></li>
<li><a href="more.html#user-content-310-embeding-tomcat"><span class="glyphicon glyphicon-th-list"></span> Embeding Tomcat</a></li>
<li><a href="more.html#user-content-311-more-about-nginx-worker-process"><span class="glyphicon glyphicon-th-list"></span> More about Nginx Worker Process</a></li>
<li><a href="more.html"><span class="glyphicon glyphicon-th-list"></span> More about Nginx-Clojure</a></li>
<li><a href="api/"><span class="glyphicon glyphicon-book text-warning"></span> API Reference (Clojure)</a></li>
</ul>
</div>
<a href="userfullLinks.html" class="btn btn-sample btn-md"><span class="glyphicon glyphicon-share-alt"></span> Useful Links</a>
</div>
</nav>
</div>
<!-- MAIN CONTENT -->
<div id="main_content_wrap" class="outer">
<section id="main_content" class="inner">
<h1>
<a id="user-content-3-more-about-nginx-clojure" class="anchor" href="#3-more-about-nginx-clojure" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>3. More about Nginx-Clojure</h1>
<h2>
<a id="user-content-30-more-about-apis" class="anchor" href="#30-more-about-apis" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>3.0 More about APIs</h2>
<h3>
<a id="user-content-request--response" class="anchor" href="#request--response" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Request & Response</h3>
<p>For Clojure the request map and response map are defined by the ring SEPC at <a href="https://github.com/ring-clojure/ring/blob/master/SPEC">https://github.com/ring-clojure/ring/blob/master/SPEC</a> .</p>
<p>For Java/Groovy, the request map contains serveral parts:</p>
<ol>
<li>server-port (Required, Integer) The port on which the request is being handled.</li>
<li>server-name (Required, String) The resolved server name, or the server IP address.</li>
<li>remote-addr (Required, String) The IP address of the client or the last proxy that sent the request.</li>
<li>uri (Required, String) The request URI, excluding the query string and the "?" separator. Must start with "/".</li>
<li>query-string (Optional, String) The query string, if present.</li>
<li>scheme (Required, String) The transport protocol, must be http or https.</li>
<li>request-method (Required, String) The HTTP request method, must be a lowercase keyword corresponding to a HTTP request method, such as :get or :post.</li>
<li>content-type <strong>DEPRECATED</strong> (Optional, String) The MIME type of the request body, if known.</li>
<li>content-length <strong>DEPRECATED</strong> (Optional, Integer) The number of bytes in the request body, if known.</li>
<li>character-encoding <strong>DEPRECATED</strong> (Optional, String) The name of the character encoding used in the request body, if known.</li>
<li>sl-client-cert (Optional, X509Certificate) The SSL client certificate, if supplied. This value is not <strong>supported</strong> yet.</li>
<li>headers (Required, Map) A map of header name Strings to corresponding header value Strings.</li>
<li>body (Optional, InputStream) An InputStream for the request body, if present.</li>
</ol>
<p>The return response is an array of object, e.g</p>
<div class="highlight highlight-source-java"><pre>
[<span class="pl-c1">200</span>, <span class="pl-c">//http status 200 </span>
<span class="pl-smi">ArrayMap</span><span class="pl-k">.</span>create(<span class="pl-s"><span class="pl-pds">"</span>Content-Type<span class="pl-pds">"</span></span>, <span class="pl-s"><span class="pl-pds">"</span>text/html<span class="pl-pds">"</span></span>, <span class="pl-s"><span class="pl-pds">"</span><span class="pl-pds">"</span></span>, <span class="pl-s"><span class="pl-pds">"</span><span class="pl-pds">"</span></span> ), <span class="pl-c">//headers map</span>
<span class="pl-s"><span class="pl-pds">"</span>Hello, Java & Nginx!<span class="pl-pds">"</span></span> <span class="pl-c">//response body can be string, File or Array/Collection of string or File ]; </span></pre></div>
<blockquote>
<p>Note that if the rewrite/access handler returns phase-done (Clojure) or Constants.PHRASE_DONE (Groovy/Java), nginx will continue to next phases (e.g. invoke proxy_pass or content ring handler). If the rewrite handler returns a general response, nginx will send this response to the client and stop to continue to next phases.</p>
</blockquote>
<h2>
<a id="user-content-31-handle-multiple-coroutine-based-sockets-parallel" class="anchor" href="#31-handle-multiple-coroutine-based-sockets-parallel" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>3.1 Handle Multiple Coroutine Based Sockets Parallel</h2>
<p>Sometimes we need to invoke serveral remote services before completing the ring response. For better performance we need a way to handle multiple sockets parallel in sub coroutines.</p>
<p>e.g. fetch two page parallel by clj-http</p>
<div class="highlight highlight-source-clojure"><pre> (<span class="pl-k">let</span> [[r1, r2]
(<span class="pl-en">co-pvalues</span> (<span class="pl-en">client/get</span> <span class="pl-s"><span class="pl-pds">"</span>http://service1-url<span class="pl-pds">"</span></span>)
(<span class="pl-en">client/get</span> <span class="pl-s"><span class="pl-pds">"</span>http://service2-url<span class="pl-pds">"</span></span>))]
<span class="pl-c">;println bodies of two remote response</span>
(<span class="pl-en">println</span> (<span class="pl-en">str</span> (<span class="pl-c1">:body</span> r1) <span class="pl-s"><span class="pl-pds">"</span>====<span class="pl-cce">\n</span><span class="pl-pds">"</span></span> (<span class="pl-c1">:body</span> r2) ))</pre></div>
<p>Here <code>co-pvalues</code> is also non-blocking and coroutine based. In fact it will create two sub coroutines to handle two sockets.</p>
<p>For Java/Groovy, we can use <code>NginxClojureRT.coBatchCall</code> to do the same thing. Here's a simple example for Groovy.</p>
<div class="highlight highlight-source-groovy"><pre> <span class="pl-k">def</span> (r1, r2) <span class="pl-k">=</span> <span class="pl-k">NginxClojureRT</span><span class="pl-k">.</span>coBatchCall(
{<span class="pl-s"><span class="pl-pds">"</span>http://mirror.bit.edu.cn/apache/httpcomponents/httpclient/<span class="pl-pds">"</span></span><span class="pl-k">.</span>toURL()<span class="pl-k">.</span>text},
{<span class="pl-s"><span class="pl-pds">"</span>http://mirror.bit.edu.cn/apache/httpcomponents/httpcore/<span class="pl-pds">"</span></span><span class="pl-k">.</span>toURL()<span class="pl-k">.</span>text})
<span class="pl-k">return</span> [<span class="pl-c1">200</span>, [<span class="pl-s"><span class="pl-pds">"</span>Content-Type<span class="pl-pds">"</span></span>:<span class="pl-s"><span class="pl-pds">"</span>text/html<span class="pl-pds">"</span></span>], r1 <span class="pl-k">+</span> r2];
</pre></div>
<h2>
<a id="user-content-32-shared-map-among-nginx-workers" class="anchor" href="#32-shared-map-among-nginx-workers" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>3.2 Shared Map among Nginx Workers</h2>
<p>See <a href="sharedmap.html">Shared Map & Session Store</a>.</p>
<h2>
<a id="user-content-33-user-defined-http-method" class="anchor" href="#33-user-defined-http-method" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>3.3 User Defined Http Method</h2>
<p>Some web services need a user defined http request method to define special operations beyond standard http request methods. </p>
<p>e.g. We use <code>MYUPLOAD</code> to upload a file and overwrite the one if it exists. The <code>curl</code> command could be </p>
<div class="highlight highlight-source-shell"><pre>curl -v -X MYUPLOAD --upload-file post-test-data \
<span class="pl-s"><span class="pl-pds">"</span>http://localhost:8080/myservice<span class="pl-pds">"</span></span> </pre></div>
<p>In the nginx.conf, we can use <code>always_read_body on;</code> to force nginx to read http body.</p>
<div class="highlight highlight-source-nginx"><pre>
<span class="pl-k">location</span> <span class="pl-en">/myservice </span>{
<span class="pl-k">handler_type</span> <span class="pl-s">'clojure'</span>;
<span class="pl-k">always_read_body</span> on;
<span class="pl-k">content_handler_code</span> <span class="pl-s">'....'</span>;
}
</pre></div>
<h2>
<a id="user-content-34-server-channel-for-long-polling--server-sent-events-sse" class="anchor" href="#34-server-channel-for-long-polling--server-sent-events-sse" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>3.4 Server Channel for Long Polling & Server Sent Events (SSE)</h2>
<p>Since v0.2.5, nginx-clojure provides a union form of <a href="https://github.com/nginx-clojure/nginx-clojure/issues/41">hijack API</a> to work with Long Polling & Server Sent Events (SSE).</p>
<h3>
<a id="user-content-hijack-the-request" class="anchor" href="#hijack-the-request" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Hijack the Request</h3>
<p>We can hijack the request to get a http server channel to send some messages later. After hijacking the return result from ring handler, it will be ignored so we can finely control when & what to be sent to the client.</p>
<p>For Clojure</p>
<div class="highlight highlight-source-clojure"><pre>
(<span class="pl-k">fn</span> my-handler[req]
(<span class="pl-k">let</span> [ch (<span class="pl-en">hijack!</span> req <span class="pl-c1">true</span>)]
<span class="pl-c">;;; save channel ch to use it later </span>
))</pre></div>
<p>The complete <code>hijack!</code> description is below </p>
<div class="highlight highlight-source-clojure"><pre>
(<span class="pl-k">defn</span> <span class="pl-e">hijack!</span>
<span class="pl-s"><span class="pl-pds">"</span>Hijack a nginx request and return a server channel.</span>
<span class="pl-s"> After being hijacked, the ring handler's result will be ignored.</span>
<span class="pl-s"> If ignore-nginx-filter? is true all data output to channel won't be filtered</span>
<span class="pl-s"> by any nginx HTTP header/body filters such as gzip filter, chucked filter, etc.</span>
<span class="pl-s"> We can use this function to implement long polling / Server Sent Events (SSE) easily.<span class="pl-pds">"</span></span>
[^NginxRequest req ignore-nginx-filter?])</pre></div>
<p>For Java</p>
<div class="highlight highlight-source-java"><pre>
<span class="pl-k">public</span> <span class="pl-k">class</span> <span class="pl-en">MyHandler</span> <span class="pl-k">implements</span> <span class="pl-e">NginxJavaRingHandler</span> {
<span class="pl-k">@Override</span>
<span class="pl-k">public</span> <span class="pl-k">Object</span>[] <span class="pl-en">invoke</span>(<span class="pl-k">Map<<span class="pl-smi">String</span>, <span class="pl-smi">Object</span>></span> <span class="pl-v">request</span>) {
<span class="pl-smi">NginxJavaRequest</span> r <span class="pl-k">=</span> ((<span class="pl-smi">NginxJavaRequest</span>)request);
<span class="pl-smi">NginxHttpServerChannel</span> channel <span class="pl-k">=</span> r<span class="pl-k">.</span>handler()<span class="pl-k">.</span>hijack(r, <span class="pl-c1">true</span>);
<span class="pl-c">//save channel ch to use it later </span>
<span class="pl-c">//nginx-clojure will ignore this return because we have hijacked the request.</span>
<span class="pl-k">return</span> <span class="pl-c1">null</span>;
}
}</pre></div>
<p>The complete java doc about hijack is below</p>
<div class="highlight highlight-source-java"><pre> <span class="pl-c">/**</span>
<span class="pl-c"> * Get a hijacked Server Channel used to send message later typically in another thread</span>
<span class="pl-c"> * If ignoreFilter is true all data output to channel won't be filtered</span>
<span class="pl-c"> * by any nginx HTTP header/body filters such as gzip filter, chucked filter, etc.</span>
<span class="pl-c"> * @param req the request object</span>
<span class="pl-c"> * @param ignoreFilter whether we need ignore nginx filter or not.</span>
<span class="pl-c"> * @return hijacked channel used to send message later</span>
<span class="pl-c"> */</span>
<span class="pl-k">public</span> <span class="pl-smi">NginxHttpServerChannel</span> hijack(<span class="pl-smi">NginxRequest</span> req, <span class="pl-k">boolean</span> ignoreFilter);</pre></div>
<h3>
<a id="user-content-send-a-complete-response-for-long-polling" class="anchor" href="#send-a-complete-response-for-long-polling" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Send a Complete Response for Long Polling</h3>
<p>When an event happens which produces a complete response it must be sent to the Long Polling request client. We can use <code>send-response!</code>(Clojure) or sendResponse (Java/Groovy) to send a complete response. This action is non-blocking and after completion the channel will be closed automatically.</p>
<p>For Clojure</p>
<div class="highlight highlight-source-clojure"><pre>
(<span class="pl-en">send-response!</span> ch {<span class="pl-c1">:status</span> <span class="pl-c1">200</span>, <span class="pl-c1">:headers</span> {<span class="pl-s"><span class="pl-pds">"</span>content-type<span class="pl-pds">"</span></span> <span class="pl-s"><span class="pl-pds">"</span>text/plain<span class="pl-pds">"</span></span>}, <span class="pl-c1">:body</span> data}</pre></div>
<p>For Java</p>
<div class="highlight highlight-source-java"><pre>
channel<span class="pl-k">.</span>sendResponse(<span class="pl-k">new</span> <span class="pl-smi">Object</span>[] { <span class="pl-c1">NGX_HTTP_OK</span>,
<span class="pl-smi">ArrayMap</span><span class="pl-k">.</span>create(<span class="pl-s"><span class="pl-pds">"</span>content-type<span class="pl-pds">"</span></span>, <span class="pl-s"><span class="pl-pds">"</span>text/json<span class="pl-pds">"</span></span>),
message});</pre></div>
<h3>
<a id="user-content-send-messages-for-server-sent-events-sse" class="anchor" href="#send-messages-for-server-sent-events-sse" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Send Messages for Server Sent Events (SSE)</h3>
<p>First we can use <code>send-headers!</code>(Clojure) or <code>sendHeaders</code> (Java/Groovy) to send a SSE header. Then we
can use <code>send!</code> (Clojure) or <code>send</code> (Java/Groovy) to send later messages. The last two parameters of a send
function is used to flush a message or close the channel after sending the current message.</p>
<p>For Clojure:</p>
<div class="highlight highlight-source-clojure"><pre>
<span class="pl-c">;;; send header and retry hint of SSE</span>
(<span class="pl-en">send-header!</span> ch <span class="pl-c1">200</span> {<span class="pl-s"><span class="pl-pds">"</span>Content-Type<span class="pl-pds">"</span></span>, <span class="pl-s"><span class="pl-pds">"</span>text/event-stream<span class="pl-pds">"</span></span>} <span class="pl-c1">false</span> <span class="pl-c1">false</span>)
(<span class="pl-en">send!</span> ch <span class="pl-s"><span class="pl-pds">"</span>retry: 4500<span class="pl-cce">\r\n</span><span class="pl-pds">"</span></span> <span class="pl-c1">true</span> <span class="pl-c1">false</span>)
<span class="pl-c">;;; send the message and do flush </span>
(<span class="pl-en">send!</span> ch <span class="pl-s"><span class="pl-pds">"</span>data: Are you ok?<span class="pl-cce">\r\n</span><span class="pl-pds">"</span></span> <span class="pl-c1">true</span> <span class="pl-c1">false</span>)
<span class="pl-c">;;; send the last message, identical to (send! ch data true false) (close! ch)</span>
(<span class="pl-en">send!</span> ch <span class="pl-s"><span class="pl-pds">"</span>data: Bye, bye.<span class="pl-cce">\r\n</span><span class="pl-pds">"</span></span> <span class="pl-c1">true</span> <span class="pl-c1">true</span>)
</pre></div>
<p>For Java:</p>
<div class="highlight highlight-source-java"><pre>
<span class="pl-c">//send header and retry hint of SSE</span>
channel<span class="pl-k">.</span>sendHeader(<span class="pl-c1">200</span>, <span class="pl-smi">ArrayMap</span><span class="pl-k">.</span>create(<span class="pl-s"><span class="pl-pds">"</span>Content-Type<span class="pl-pds">"</span></span>, <span class="pl-s"><span class="pl-pds">"</span>text/event-stream<span class="pl-pds">"</span></span>)<span class="pl-k">.</span>entrySet(), <span class="pl-c1">true</span>, <span class="pl-c1">false</span>);
channel<span class="pl-k">.</span>send(<span class="pl-s"><span class="pl-pds">"</span>retry: 4500<span class="pl-cce">\r\n</span><span class="pl-pds">"</span></span>, <span class="pl-c1">true</span>, <span class="pl-c1">false</span>);
<span class="pl-c">//send the message and do flush </span>
channel<span class="pl-k">.</span>send(<span class="pl-s"><span class="pl-pds">"</span>data: Are you ok?<span class="pl-cce">\r\n</span><span class="pl-pds">"</span></span>, <span class="pl-c1">true</span>, <span class="pl-c1">false</span>)
<span class="pl-c">//send the last message, identical to channel.send(data true false); channel.close();</span>
channel<span class="pl-k">.</span>send(<span class="pl-s"><span class="pl-pds">"</span>data: Bye, bye.<span class="pl-cce">\r\n</span><span class="pl-pds">"</span></span>, <span class="pl-c1">true</span>, <span class="pl-c1">true</span>)</pre></div>
<h3>
<a id="user-content-listener-about-the-closed-event-of-channel" class="anchor" href="#listener-about-the-closed-event-of-channel" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Listener about the Closed Event of Channel</h3>
<p>A closed event will happen immediately when a channel is closed by either of these three cases:</p>
<ul>
<li>channel close function/method is invoked on this channel, e.g. (close! ch)</li>
<li>inner unrecoverable error happens with this channel, e.g. not enough memory to read/write</li>
<li>remote client connection is closed or broken.</li>
</ul>
<p>For Clojure</p>
<div class="highlight highlight-source-clojure"><pre>
(<span class="pl-en">on-close!</span> ch {<span class="pl-c1">:ch</span> ch <span class="pl-c1">:desc</span> <span class="pl-s"><span class="pl-pds">"</span>this is an event attachement<span class="pl-pds">"</span></span>}
(<span class="pl-en">fn[att]</span> (<span class="pl-en">info</span> <span class="pl-s"><span class="pl-pds">"</span>closed channel from request :<span class="pl-pds">"</span></span> (<span class="pl-en">.request</span> (<span class="pl-c1">:ch</span> att)))))</pre></div>
<p>For Java</p>
<div class="highlight highlight-source-java"><pre>
channel<span class="pl-k">.</span>addListener(channel, <span class="pl-k">new</span> <span class="pl-k">ChannelCloseAdapter<<span class="pl-smi">NginxHttpServerChannel</span>></span>() {
<span class="pl-k">@Override</span>
<span class="pl-k">public</span> <span class="pl-k">void</span> <span class="pl-en">onClose</span>(<span class="pl-smi">NginxHttpServerChannel</span> <span class="pl-v">data</span>) {
<span class="pl-smi">Init</span><span class="pl-k">.</span>serverSentEventSubscribers<span class="pl-k">.</span>remove(data);
info(<span class="pl-s"><span class="pl-pds">"</span>closed <span class="pl-pds">"</span></span> <span class="pl-k">+</span> data<span class="pl-k">.</span>request()<span class="pl-k">.</span>nativeRequest());
}
});</pre></div>
<h2>
<a id="user-content-35-subpub--broadcast-event" class="anchor" href="#35-subpub--broadcast-event" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>3.5 Sub/Pub & broadcast Event</h2>
<p>Suppose our Nginx instance has 3 workers (worker process not jvm_workers which is just thread number of thread pool in jvm). Now we want to provide a sub/pub service. e.g.</p>
<ol>
<li>Client A connected to nginx worker A and subscribed to uri <code>/mychannel/sub</code>
</li>
<li>Client B connected to nginx worker B and subscribed to uri <code>/mychannel/sub</code>
</li>
<li>Client C connected to nginx worker C and publish a message to uri <code>/mychannel/pub</code>
</li>
</ol>
<p>So the service at endpoint of <code>/mychannel/pub</code> must broadcast a pub event to Client A and Client B.
Although for large-scale application we can use a sub/pub service from Redis on nginx-clojure. For small-scale or medium-scale applications this feature will make the dev's life easier.</p>
<p>More details can be found at <a href="subpub.html">Pub/Sub Among Nginx Worker Processes</a></p>
<h2>
<a id="user-content-36-asynchronous-client-channel" class="anchor" href="#36-asynchronous-client-channel" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>3.6 Asynchronous Client Channel</h2>
<p>Asynchronous Client Channel is a wrapper of Asynchronous Client Socket made for more easier usage.
So far Asynchronous Channel <em>can't</em> work with thread pool mode. The Asynchronous Channel
API is similar to Java 7 NIO.2 Asynchronous Channel and more details can be found from issue #37 and its comments
<a href="https://github.com/nginx-clojure/nginx-clojure/issues/37">Asynchronous Channel API</a>.</p>
<p>Here's an example which is retrieving content from mirror.bit.edu.cn:8080 and sending it to the client.</p>
<ul>
<li><a href="https://github.com/nginx-clojure/nginx-clojure/blob/master/test/clojure/nginx/clojure/asyn_channel_handlers_for_test.clj">Clojure Example</a></li>
<li><a href="https://github.com/nginx-clojure/nginx-clojure/blob/master/test/java/nginx/clojure/net/SimpleHandler4TestNginxClojureAsynChannel.java">Java Example</a></li>
</ul>
<h2>
<a id="user-content-37--about-logging" class="anchor" href="#37--about-logging" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>3.7 About Logging</h2>
<p>For logging with nginx-clojure there are several ways:</p>
<ol>
<li>Using System.err.print/println will write a log to nginx error.log. This way is simplest but logging information will be mixed if you have more than one nginx worker.</li>
<li>Using clojure tools.logging + logback or slf4j + logback, we can get one log file per nginx worker.</li>
</ol>
<p>e.g</p>
<p>in nginx.conf</p>
<div class="highlight highlight-source-nginx"><pre>
<span class="pl-k">jvm_options</span> <span class="pl-s">"-DMYPID=#{pno}"</span>;</pre></div>
<p>in logback.xml</p>
<div class="highlight highlight-text-xml"><pre>
<<span class="pl-ent">appender</span> <span class="pl-e">name</span>=<span class="pl-s"><span class="pl-pds">"</span>FILE<span class="pl-pds">"</span></span> <span class="pl-e">class</span>=<span class="pl-s"><span class="pl-pds">"</span>ch.qos.logback.core.rolling.RollingFileAppender<span class="pl-pds">"</span></span>>
<<span class="pl-ent">rollingPolicy</span> <span class="pl-e">class</span>=<span class="pl-s"><span class="pl-pds">"</span>ch.qos.logback.core.rolling.TimeBasedRollingPolicy<span class="pl-pds">"</span></span>>
<span class="pl-c"><!-- daily rollover --></span>
<<span class="pl-ent">fileNamePattern</span>>logs/myapp.%d{yyyy-MM-dd}-${MYPID}.log</<span class="pl-ent">fileNamePattern</span>>
<span class="pl-c"><!-- keep 30 days' worth of history --></span>
<<span class="pl-ent">maxHistory</span>>30</<span class="pl-ent">maxHistory</span>>
</<span class="pl-ent">rollingPolicy</span>>
<<span class="pl-ent">encoder</span>>
<<span class="pl-ent">pattern</span>>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %-10contextName %logger{36} - %msg%n</<span class="pl-ent">pattern</span>>
</<span class="pl-ent">encoder</span>>
</<span class="pl-ent">appender</span>></pre></div>
<p>Then we'll generate log files named in this convention: myapp.2014-09-12-1.log, myapp.2014-09-12-2.log and so on.</p>
<h2>
<a id="user-content-38--sever-side-websocket" class="anchor" href="#38--sever-side-websocket" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>3.8 Sever Side WebSocket</h2>
<h3>
<a id="user-content-381-echo-service-example" class="anchor" href="#381-echo-service-example" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>3.8.1 Echo Service Example</h3>
<p>Sever Side WebSocket, like long polling/Server Sent Events, also use hijack API to get a NginxHttpServerChannel to send / receive messages.
Here we give a echo service example.</p>
<p>In nginx.conf</p>
<div class="highlight highlight-source-nginx"><pre><span class="pl-k">location</span> <span class="pl-en">/my-ws </span>{
<span class="pl-k">auto_upgrade_ws</span> on;
<span class="pl-k">content_handler_type</span> java; <span class="pl-c">###or clojure,groovy</span>
<span class="pl-k">content_handler_name</span> <span class="pl-s">'nginx.clojure.java.WSEcho'</span>; <span class="pl-c">###or ring handler for clojure</span>
.....
}</pre></div>
<p>The directive <code>auto_upgrade_ws on</code> is equivalent to </p>
<div class="highlight highlight-source-java"><pre> <span class="pl-c">//NginxHttpServerChannel sc = r.hijack(true);</span>
<span class="pl-c">//If we use nginx directive `auto_upgrade_ws on;`, these three lines can be omitted.</span>
<span class="pl-k">if</span> (<span class="pl-k">!</span>sc<span class="pl-k">.</span>webSocketUpgrade(<span class="pl-c1">true</span>)) {
<span class="pl-k">return</span> <span class="pl-c1">null</span>;
}</pre></div>
<p>For clojure</p>
<div class="highlight highlight-source-clojure"><pre>(<span class="pl-k">defn</span> <span class="pl-e">echo</span> [^NginxRequest req]
(<span class="pl-en">-></span> req
(<span class="pl-en">hijack!</span> <span class="pl-c1">true</span>)
(<span class="pl-en">add-listener!</span> { <span class="pl-c1">:on-open</span> (<span class="pl-k">fn</span> [ch] (<span class="pl-en">log</span> <span class="pl-s"><span class="pl-pds">"</span>uri:%s, on-open!<span class="pl-pds">"</span></span> (<span class="pl-c1">:uri</span> req)))
<span class="pl-c1">:on-message</span> (<span class="pl-k">fn</span> [ch msg rem?] (<span class="pl-en">send!</span> ch msg (<span class="pl-en">not</span> rem?) <span class="pl-c1">false</span>))
<span class="pl-c1">:on-close</span> (<span class="pl-k">fn</span> [ch reason] (<span class="pl-en">log</span> <span class="pl-s"><span class="pl-pds">"</span>uri:%s, on-close:%s<span class="pl-pds">"</span></span> (<span class="pl-c1">:uri</span> req) reason))
<span class="pl-c1">:on-error</span> (<span class="pl-k">fn</span> [ch error] (<span class="pl-en">log</span> <span class="pl-s"><span class="pl-pds">"</span>uri:%s, on-error:%s<span class="pl-pds">"</span></span> (<span class="pl-c1">:uri</span> req) error))
})))</pre></div>
<p>For java </p>
<div class="highlight highlight-source-java"><pre><span class="pl-k">package</span> <span class="pl-smi">nginx.clojure.java</span>;
<span class="pl-k">import</span> <span class="pl-smi">java.io.IOException</span>;
<span class="pl-k">import</span> <span class="pl-smi">java.nio.ByteBuffer</span>;
<span class="pl-k">import</span> <span class="pl-smi">java.util.Map</span>;
<span class="pl-k">import</span> <span class="pl-smi">nginx.clojure.MessageAdapter</span>;
<span class="pl-k">import</span> <span class="pl-smi">nginx.clojure.NginxClojureRT</span>;
<span class="pl-k">import</span> <span class="pl-smi">nginx.clojure.NginxHttpServerChannel</span>;
<span class="pl-k">public</span> <span class="pl-k">class</span> <span class="pl-en">WSEcho</span> <span class="pl-k">implements</span> <span class="pl-e">NginxJavaRingHandler</span> {
<span class="pl-k">@Override</span>
<span class="pl-k">public</span> <span class="pl-k">Object</span>[] <span class="pl-en">invoke</span>(<span class="pl-k">Map<<span class="pl-smi">String</span>, <span class="pl-smi">Object</span>></span> <span class="pl-v">request</span>) {
<span class="pl-smi">NginxJavaRequest</span> r <span class="pl-k">=</span> (<span class="pl-smi">NginxJavaRequest</span>)request;
<span class="pl-smi">NginxHttpServerChannel</span> sc <span class="pl-k">=</span> r<span class="pl-k">.</span>hijack(<span class="pl-c1">true</span>);
sc<span class="pl-k">.</span>addListener(sc, <span class="pl-k">new</span> <span class="pl-k">MessageAdapter<<span class="pl-smi">NginxHttpServerChannel</span>></span>() {
<span class="pl-k">int</span> total <span class="pl-k">=</span> <span class="pl-c1">0</span>;
<span class="pl-k">@Override</span>
<span class="pl-k">public</span> <span class="pl-k">void</span> <span class="pl-en">onOpen</span>(<span class="pl-smi">NginxHttpServerChannel</span> <span class="pl-v">data</span>) {
<span class="pl-smi">NginxClojureRT</span><span class="pl-k">.</span>log<span class="pl-k">.</span>debug(<span class="pl-s"><span class="pl-pds">"</span>WSEcho onOpen!<span class="pl-pds">"</span></span>);
}
<span class="pl-k">@Override</span>
<span class="pl-k">public</span> <span class="pl-k">void</span> <span class="pl-en">onTextMessage</span>(<span class="pl-smi">NginxHttpServerChannel</span> <span class="pl-v">sc</span>, <span class="pl-smi">String</span> <span class="pl-v">message</span>, <span class="pl-k">boolean</span> <span class="pl-v">remaining</span>) <span class="pl-k">throws</span> <span class="pl-smi">IOException</span> {
<span class="pl-k">if</span> (<span class="pl-smi">NginxClojureRT</span><span class="pl-k">.</span>log<span class="pl-k">.</span>isDebugEnabled()) {
<span class="pl-smi">NginxClojureRT</span><span class="pl-k">.</span>log<span class="pl-k">.</span>debug(<span class="pl-s"><span class="pl-pds">"</span>WSEcho onTextMessage: msg=%s, rem=%s<span class="pl-pds">"</span></span>, message, remaining);
}
total <span class="pl-k">+=</span> message<span class="pl-k">.</span>length();
sc<span class="pl-k">.</span>send(message, <span class="pl-k">!</span>remaining, <span class="pl-c1">false</span>);
}
<span class="pl-k">@Override</span>
<span class="pl-k">public</span> <span class="pl-k">void</span> <span class="pl-en">onBinaryMessage</span>(<span class="pl-smi">NginxHttpServerChannel</span> <span class="pl-v">sc</span>, <span class="pl-smi">ByteBuffer</span> <span class="pl-v">message</span>, <span class="pl-k">boolean</span> <span class="pl-v">remining</span>) <span class="pl-k">throws</span> <span class="pl-smi">IOException</span> {
<span class="pl-k">if</span> (<span class="pl-smi">NginxClojureRT</span><span class="pl-k">.</span>log<span class="pl-k">.</span>isDebugEnabled()) {
<span class="pl-smi">NginxClojureRT</span><span class="pl-k">.</span>log<span class="pl-k">.</span>debug(<span class="pl-s"><span class="pl-pds">"</span>WSEcho onBinaryMessage: msg=%s, rem=%s, total=%d<span class="pl-pds">"</span></span>, message, remining, total);
}
total <span class="pl-k">+=</span> message<span class="pl-k">.</span>remaining();
sc<span class="pl-k">.</span>send(message, <span class="pl-k">!</span>remining, <span class="pl-c1">false</span>);
}
<span class="pl-k">@Override</span>
<span class="pl-k">public</span> <span class="pl-k">void</span> <span class="pl-en">onClose</span>(<span class="pl-smi">NginxHttpServerChannel</span> <span class="pl-v">req</span>, <span class="pl-k">long</span> <span class="pl-v">status</span>, <span class="pl-smi">String</span> <span class="pl-v">reason</span>) {
<span class="pl-k">if</span> (<span class="pl-smi">NginxClojureRT</span><span class="pl-k">.</span>log<span class="pl-k">.</span>isDebugEnabled()) {
<span class="pl-smi">NginxClojureRT</span><span class="pl-k">.</span>log<span class="pl-k">.</span>info(<span class="pl-s"><span class="pl-pds">"</span>WSEcho onClose2: total=%d, status=%d, reason=%s<span class="pl-pds">"</span></span>, total, status, reason);
}
}
<span class="pl-k">@Override</span>
<span class="pl-k">public</span> <span class="pl-k">void</span> <span class="pl-en">onError</span>(<span class="pl-smi">NginxHttpServerChannel</span> <span class="pl-v">data</span>, <span class="pl-k">long</span> <span class="pl-v">status</span>) {
<span class="pl-k">if</span> (<span class="pl-smi">NginxClojureRT</span><span class="pl-k">.</span>log<span class="pl-k">.</span>isDebugEnabled()) {
<span class="pl-smi">NginxClojureRT</span><span class="pl-k">.</span>log<span class="pl-k">.</span>info(<span class="pl-s"><span class="pl-pds">"</span>WSEcho onError: total=%d, status=%d<span class="pl-pds">"</span></span>, total, status);
}
}
});
<span class="pl-k">return</span> <span class="pl-c1">null</span>;
}
}</pre></div>
<h3>
<a id="user-content-381-use-access-handler-for-websocket-security" class="anchor" href="#381-use-access-handler-for-websocket-security" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>3.8.1 Use Access Handler For WebSocket Security</h3>
<p>In the example below we return 404 for non WebSocket request </p>
<div class="highlight highlight-source-nginx"><pre><span class="pl-k">location</span> <span class="pl-en">/my-ws </span>{
<span class="pl-k">auto_upgrade_ws</span> on;
<span class="pl-k">access_handler_type</span> java;
<span class="pl-k">access_handler_name</span> <span class="pl-s">'my.WSAccessHandler'</span>;
<span class="pl-k">content_handler_type</span> java; <span class="pl-c">###or clojure,groovy</span>
<span class="pl-k">content_handler_name</span> <span class="pl-s">'nginx.clojure.java.WSEcho'</span>; <span class="pl-c">###or ring handler for clojure</span>
.....
}</pre></div>
<div class="highlight highlight-source-java"><pre><span class="pl-k">package</span> <span class="pl-smi">my</span>;
<span class="pl-k">import</span> <span class="pl-smi">nginx.clojure.java.NginxJavaRingHandler</span>;
<span class="pl-k">import static</span> <span class="pl-smi">nginx.clojure.java.Constants.*</span>;
<span class="pl-k">public</span> <span class="pl-k">class</span> <span class="pl-en">WSAccessHandler</span> <span class="pl-k">implements</span> <span class="pl-e">NginxJavaRingHandler</span> {
<span class="pl-k">public</span> <span class="pl-k">Object</span>[] <span class="pl-en">invoke</span>(<span class="pl-k">Map<<span class="pl-smi">String</span>, <span class="pl-smi">Object</span>></span> <span class="pl-v">request</span>) {
<span class="pl-k">if</span> (<span class="pl-c1">GET</span> <span class="pl-k">!=</span> request<span class="pl-k">.</span>get(<span class="pl-s"><span class="pl-pds">"</span>request-method<span class="pl-pds">"</span></span>)
<span class="pl-k">||</span> <span class="pl-k">!</span><span class="pl-s"><span class="pl-pds">"</span>websocket<span class="pl-pds">"</span></span><span class="pl-k">.</span>equals(request<span class="pl-k">.</span>get(<span class="pl-s"><span class="pl-pds">"</span>headers<span class="pl-pds">"</span></span>)<span class="pl-k">.</span>get(<span class="pl-s"><span class="pl-pds">"</span>upgrade<span class="pl-pds">"</span></span>))) {
<span class="pl-k">return</span> <span class="pl-k">new</span> <span class="pl-smi">Object</span>[]{<span class="pl-c1">404</span>, <span class="pl-c1">null</span>, <span class="pl-c1">null</span>};
}
<span class="pl-k">return</span> <span class="pl-c1">PHASE_DONE</span>;
}
}
</pre></div>
<h2>
<a id="user-content-39--java-standard-restful-web-services-with-jersey" class="anchor" href="#39--java-standard-restful-web-services-with-jersey" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>3.9 Java standard RESTful web services with Jersey</h2>
<ul>
<li><strong>Get Jar File</strong></li>
</ul>
<p>We can get the released version from <a href="https://clojars.org/nginx-clojure/nginx-jersey">clojars</a> or
the jar in <a href="https://sourceforge.net/projects/nginx-clojure/files/">nginx-clojure binary release</a> </p>
<p>To get the latest version from the github source</p>
<div class="highlight highlight-source-shell"><pre>git clone https://github.com/nginx-clojure/nginx-clojure
<span class="pl-c1">cd</span> nginx-clojure/nginx-jersey
lein jar</pre></div>
<ul>
<li> <strong>Configuration</strong>
</li>
</ul>
<p>in nginx.conf</p>
<div class="highlight highlight-source-nginx"><pre> <span class="pl-k">location</span> <span class="pl-en">/jersey </span>{
<span class="pl-k">content_handler_type</span> java;
<span class="pl-k">content_handler_name</span> <span class="pl-s">'nginx.clojure.bridge.NginxBridgeHandler'</span>;
<span class="pl-c">##we can set system properties ,e.g. m2rep</span>
<span class="pl-c">#content_handler_property system.m2rep '/home/who/.m2/repository';</span>
<span class="pl-c">##we can put jars into some dir then all of their path will be appended into the classpath</span>
<span class="pl-c">#content_handler_property bridge.lib.dirs 'my-jersey-libs-dir:myother-dir';</span>
<span class="pl-c">##the path of nginx-jersey-x.x.x.jar must be included in the below classpath or one of above #{bridge.lib.dirs}</span>
<span class="pl-c">##we can use maven assembly plugin to get a all-in-one jar file with dependencies, e.g. json-jackson-example-with-dependencies.jar.</span>
<span class="pl-k">content_handler_property</span> bridge.lib.cp <span class="pl-s">'jars/nginx-jersey-0.1.0.jar:myjars/json-jackson-example-with-dependencies.jar'</span>;
<span class="pl-k">content_handler_property</span> bridge.imp <span class="pl-s">'nginx.clojure.jersey.NginxJerseyContainer'</span>;
<span class="pl-c">##aplication path usually it is the same with nginx location </span>
<span class="pl-k">content_handler_property</span> jersey.app.path <span class="pl-s">'/jersey'</span>;
<span class="pl-c">##application resources which can be either of JAX-RS resources, providers</span>
<span class="pl-k">content_handler_property</span> jersey.app.resources <span class="pl-s">'</span>
<span class="pl-s"> org.glassfish.jersey.examples.jackson.EmptyArrayResource,</span>
<span class="pl-s"> org.glassfish.jersey.examples.jackson.NonJaxbBeanResource,</span>
<span class="pl-s"> org.glassfish.jersey.examples.jackson.CombinedAnnotationResource,</span>
<span class="pl-s"> org.glassfish.jersey.examples.jackson.MyObjectMapperProvider,</span>
<span class="pl-s"> org.glassfish.jersey.examples.jackson.ExceptionMappingTestResource,</span>
<span class="pl-s"> org.glassfish.jersey.jackson.JacksonFeature</span>
<span class="pl-s"> '</span>;
<span class="pl-k">gzip</span><span class="pl-c1"> on</span>;
<span class="pl-k">gzip_types</span> application/javascript application/xml text/plain text/css <span class="pl-s">'text/html;charset=UTF-8'</span>;
}</pre></div>
<p>All sources about this example can be found from jersey github repository's example <a href="https://github.com/jersey/jersey/tree/2.17/examples/json-jackson/src/main/java/org/glassfish/jersey/examples/jackson">json-jackson</a>.</p>
<p>Then we test the JAX-RS services by curl</p>
<div class="highlight highlight-source-shell"><pre>$ curl -v http://localhost:8080/jersey/emptyArrayResource
<span class="pl-k">></span> GET /jersey/emptyArrayResource HTTP/1.1
<span class="pl-k">></span> User-Agent: curl/7.35.0
<span class="pl-k">></span> Host: localhost:8080
<span class="pl-k">></span> Accept: <span class="pl-k">*</span>/<span class="pl-k">*</span>
<span class="pl-k">></span>
<span class="pl-k"><</span> HTTP/1.1 200 OK
<span class="pl-k"><</span> Date: Sat, 23 May 2015 17:47:14 GMT
<span class="pl-k"><</span> Content-Type: application/json
<span class="pl-k"><</span> Transfer-Encoding: chunked
<span class="pl-k"><</span> Connection: keep-alive
<span class="pl-k">*</span> Server nginx-clojure is not blacklisted
<span class="pl-k"><</span> Server: nginx-clojure
<span class="pl-k"><</span>
{
<span class="pl-s"><span class="pl-pds">"</span>emtpyArray<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> [ ]
}</pre></div>
<div class="highlight highlight-source-shell"><pre>$ curl -v http://localhost:8080/jersey/nonJaxbResource
<span class="pl-k">></span> GET /jersey/nonJaxbResource HTTP/1.1
<span class="pl-k">></span> User-Agent: curl/7.35.0
<span class="pl-k">></span> Host: localhost:8080
<span class="pl-k">></span> Accept: <span class="pl-k">*</span>/<span class="pl-k">*</span>
<span class="pl-k">></span>
<span class="pl-k"><</span> HTTP/1.1 200 OK
<span class="pl-k"><</span> Date: Sat, 23 May 2015 17:46:17 GMT
<span class="pl-k"><</span> Content-Type: application/javascript
<span class="pl-k"><</span> Transfer-Encoding: chunked
<span class="pl-k"><</span> Connection: keep-alive
<span class="pl-k">*</span> Server nginx-clojure is not blacklisted
<span class="pl-k"><</span> Server: nginx-clojure
<span class="pl-k"><</span>
callback({
<span class="pl-s"><span class="pl-pds">"</span>name<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> <span class="pl-s"><span class="pl-pds">"</span>non-JAXB-bean<span class="pl-pds">"</span></span>,
<span class="pl-s"><span class="pl-pds">"</span>description<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> <span class="pl-s"><span class="pl-pds">"</span>I am not a JAXB bean, just an unannotated POJO<span class="pl-pds">"</span></span>,
<span class="pl-s"><span class="pl-pds">"</span>array<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> [ 1, 1, 2, 3, 5, 8, 13, 21 ]
<span class="pl-k">*</span> Connection <span class="pl-c">#0 to host localhost left intact</span>
})</pre></div>
<h2>
<a id="user-content-310-embeding-tomcat" class="anchor" href="#310-embeding-tomcat" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>3.10 Embeding Tomcat</h2>
<h3>
<a id="user-content-which-version" class="anchor" href="#which-version" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Which Version?</h3>
<table>
<thead>
<tr>
<th>Apache Tomcat version</th>
<th>Nginx-Clojure version</th>
<th>Nginx-Tomcat8 version</th>
</tr>
</thead>
<tbody>
<tr>
<td>8.0.20</td>
<td>>=0.4.x</td>
<td>0.1.x</td>
</tr>
<tr>
<td>8.0.23,8.0.24</td>
<td>>=0.4.x</td>
<td>0.2.x</td>
</tr>
</tbody>
</table>
<h3>
<a id="user-content-get-jar-file" class="anchor" href="#get-jar-file" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Get Jar File</h3>
<p>We can get the released version from <a href="https://clojars.org/nginx-clojure/nginx-tomcat8">clojars</a> or
the jar in <a href="https://sourceforge.net/projects/nginx-clojure/files/">nginx-clojure binary release</a> </p>
<p>To get the latest version from the github source</p>
<div class="highlight highlight-source-shell"><pre>git clone https://github.com/nginx-clojure/nginx-clojure
<span class="pl-c1">cd</span> nginx-clojure/nginx-tomcat8
lein jar</pre></div>
<h3>
<a id="user-content-configuration" class="anchor" href="#configuration" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Configuration</h3>
<p>in nginx.conf</p>
<div class="highlight highlight-source-nginx"><pre> <span class="pl-k">location</span> <span class="pl-en">/ </span>{
<span class="pl-k">content_handler_type</span> java;
<span class="pl-k">content_handler_name</span> <span class="pl-s">'nginx.clojure.bridge.NginxBridgeHandler'</span>;
<span class="pl-c">##Tomcat 8 installation path</span>
<span class="pl-k">content_handler_property</span> system.catalina.home <span class="pl-s">'/home/who/share/apps/apache-tomcat-8.0.20'</span>;
<span class="pl-k">content_handler_property</span> system.catalina.base <span class="pl-s">'#{catalina.home}'</span>;
<span class="pl-c">##uncomment this to disable websocket perframe-compression</span>
<span class="pl-c">#content_handler_property system.org.apache.tomcat.websocket.DISABLE_BUILTIN_EXTENSIONS true;</span>
<span class="pl-c">##log manger</span>
<span class="pl-k">content_handler_property</span> system.java.util.logging.manager <span class="pl-s">'org.apache.juli.ClassLoaderLogManager'</span>;
<span class="pl-c">## all jars or direct child directories will be appended into the classpath of this bridge handler's class-loader</span>
<span class="pl-k">content_handler_property</span> bridge.lib.dirs <span class="pl-s">'#{catalina.home}/lib:#{catalina.home}/bin'</span>;
<span class="pl-c">##set nginx tomcat8 bridge implementation jar and other jars can also be appended here</span>
<span class="pl-k">content_handler_property</span> bridge.lib.cp <span class="pl-s">'my-jar-path/nginx-tomcat8-x.x.x.jar'</span>;
<span class="pl-c">##The implementation class of nginx-clojure bridge handler for Tomcat 8</span>
<span class="pl-k">content_handler_property</span> bridge.imp <span class="pl-s">'nginx.clojure.tomcat8.NginxTomcatBridge'</span>;
<span class="pl-c">##ignore nginx filter, default is false</span>
<span class="pl-c">#content_handler_property ignoreNginxFilter false;</span>
<span class="pl-c">##when dispatch is false tomcat servlet will be executed in main thread. By default dispatch is false</span>
<span class="pl-c">##when use websocket with tomcat it must be set true otherwise maybe deadlock will happen.</span>
<span class="pl-c">#content_handler_property dispatch false;</span>
<span class="pl-k">gzip</span><span class="pl-c1"> on</span>;
<span class="pl-k">gzip_types</span> text/plain text/css <span class="pl-s">'text/html;charset=ISO-8859-1'</span> <span class="pl-s">'text/html;charset=UTF-8'</span>;
<span class="pl-c">##if for small message, e.g. small json/websocket message write_page_size can set to be a small value</span>
<span class="pl-c">#write_page_size 2k;</span>
}</pre></div>
<h2>
<a id="user-content-session-management" class="anchor" href="#session-management" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Session Management</h2>
<p>If <code>worker_processes</code> > 1 there will be more than one jvm instances viz. more tomcat instances so to get synchronized session information we cannot use the default tomcat session manger.
Instead we may consider using one of the following:</p>
<ol>
<li>Cookied based Session Store viz. storing all session attribute information into cookies.</li>
<li>Shared HashMap among processes in the same machine ,e.g. nginx-clojure built-in <a href="https://nginx-clojure.github.io/sharedmap.html">Shared Map</a>, OpenHFT <a href="https://github.com/OpenHFT/Chronicle-Map">Chronicle Map</a>
</li>
<li>External Session Store, e.g. Redis / MySQL / Memcached Session Store</li>
</ol>
<h3>
<a id="user-content-for-performance" class="anchor" href="#for-performance" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>For Performance</h3>
<h4>
<a id="user-content-disable-tomcat-access-log" class="anchor" href="#disable-tomcat-access-log" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Disable Tomcat Access Log</h4>
<p>When we need the access log, use Nginx's access log instead of Tomcat's access log.</p>
<p>In server.xml comment out AccessLogValve configuration to disable Tomcat access log.</p>
<div class="highlight highlight-text-xml"><pre><span class="pl-c"><!--</span>
<span class="pl-c"> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"</span>
<span class="pl-c"> prefix="localhost_access_log" suffix=".txt"</span>
<span class="pl-c"> pattern="%h %l %u %t &quot;%r&quot; %s %b" /></span>
<span class="pl-c">--></span></pre></div>
<h4>
<a id="user-content-disable-logging-to-console" class="anchor" href="#disable-logging-to-console" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Disable logging to console</h4>
<p>Because Tomcat console log is duplicated with other logs such as catalina log, manager log, etc, it can be disabled.
In conf/logging.properties remove all <code>java.util.logging.ConsoleHandler</code></p>
<div class="highlight highlight-source-shell"><pre>handlers = 1catalina.org.apache.juli.AsyncFileHandler, 2localhost.org.apache.juli.AsyncFileHandler, 3manager.org.apache.juli.AsyncFileHandler, 4host-manager.org.apache.juli.AsyncFileHandler
.handlers = 1catalina.org.apache.juli.AsyncFileHandler</pre></div>
<h4>
<a id="user-content-dont-enable-tomcat-compression" class="anchor" href="#dont-enable-tomcat-compression" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Don't Enable Tomcat Compression</h4>
<p>By default compression is off, do not turn it on.</p>
<div class="highlight highlight-text-xml"><pre><<span class="pl-ent">Connector</span> <span class="pl-e">port</span>=<span class="pl-s"><span class="pl-pds">"</span>8080<span class="pl-pds">"</span></span> <span class="pl-e">protocol</span>=<span class="pl-s"><span class="pl-pds">"</span>HTTP/1.1<span class="pl-pds">"</span></span> <span class="pl-e">compression</span>=<span class="pl-s"><span class="pl-pds">"</span>off<span class="pl-pds">"</span></span></pre></div>
<p>If we need compression use nginx gzip filter instead. e.g. In nginx.conf</p>
<div class="highlight highlight-source-nginx"><pre><span class="pl-k">location</span> <span class="pl-en">/examples </span>{
<span class="pl-k">gzip</span><span class="pl-c1"> on</span>;
<span class="pl-k">gzip_types</span> text/plain text/css <span class="pl-s">'text/html;charset=ISO-8859-1'</span> <span class="pl-s">'text/html;charset=UTF-8'</span>;
}</pre></div>
<p><code>gzip</code> can also appear under http and server blocks. More details can be found <a href="http://nginx.org/en/docs/http/ngx_http_gzip_module.html">HERE</a></p>
<h2>
<a id="user-content-311-more-about-nginx-worker-process" class="anchor" href="#311-more-about-nginx-worker-process" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>3.11 More about Nginx Worker Process</h2>
<p>The number of Nginx worker processes can be configured by nginx directive <a href="http://nginx.org/en/docs/ngx_core_module.html#worker_processes">worker_processes</a>.
e.g. We use 8 Nginx worker processes:</p>
<div class="highlight highlight-source-nginx"><pre><span class="pl-k">worker_processes</span> <span class="pl-s">8</span>;</pre></div>
<p>Because a JVM instance will be embeded into every Nginx worker process, if there are more than one Nginx worker prcocesses then there will be more than one JVM instances.
So if we want to have synchronized session information we cannot use the default tomcat session manger. Instead we may consider using one of the following: </p>
<ol>
<li>Cookie based Session Store viz. storing all session attribute information into cookies.</li>
<li>Shared HashMap among processes in the same machine ,e.g. nginx-clojure built-in <a href="https://nginx-clojure.github.io/sharedmap.html">Shared Map</a>, OpenHFT <a href="https://github.com/OpenHFT/Chronicle-Map">Chronicle Map</a>
</li>
<li>External Session Store, e.g. Redis / MySQL / Memcached Session Store</li>
</ol>
<p>When there are is than one Nginx worker process, sub/pub services also need to be taken care for. Subscribers may connect to JVM instances which may be different
than the JVM instance which the publisher sent a message to. So we need to use <a href="more.html#35-subpub--broadcast-event">broadcast API</a> from nginx-clojure
and broadcast messages to all Nginx worker processes.</p>
</section>
</div>
<div id="footer">
<!-- FOOTER -->
<div id="footer_wrap" class="outer">
<footer class="inner">
<p>
Powered by <a href="http://getbootstrap.com/">Twitter Bootstrap</a>
</p>
</footer>
</div>
</div>
<script type="text/javascript">
//$("a[href^='http']").attr("target","_blank");
//setTimeout("fixNavbarIssue();", 1000);
</script>
</body>
</html>