Java Reference
In-Depth Information
new
InputStreamReader
(
System
.
in
));
String message
=
br
.
readLine
();
target
.
request
().
post
(
Entity
.
text
(
name
+
": "
+
message
));
new
}
We simply read from
stdin
until the user hits Enter and then do an HTTP POST request to
the chat service with the command-line input.
The Server Code
The server side is doing a lot of different things to implement our chat service. Let's break it
down:
src/main/java/com/restfully/shop/services/CustomerChat.java
@Path
(
"chat"
)
public
public class
class
CustomerChat
CustomerChat
{
class
class
Message
Message
{
String id
;
String message
;
Message next
;
}
The
CustomerChat
class is annotated with
@Path
to specify the root resource path of our
JAX-RS service. It then declares a simple inner class called
Message
that will represent the
queued chat messages. A message is represented by a
String id
and a
String message
,
and also contains a reference to the next queued
Message
.
protected
protected
Message first
;
protected
protected
Message last
;
The service remembers what the current first and last message is. It stores these in the
first
and
last
member variables of the class.
protected
protected
int
int
maxMessages
=
10
;
protected
protected
LinkedHashMap
<
String
,
Message
>
messages
=
new
new
LinkedHashMap
<
String
,
Message
>()
{
@Override
protected
protected
boolean
boolean
removeEldestEntry
(
Map
.
Entry
<
String
,
Message
>
eldest
)
{
boolean
boolean
remove
=
size
() >
maxMessages
;
iif
(
remove
)
first
=
eldest
.
getValue
().
next
;
return
return
remove
;