1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.codec.marshalling;
17
18 import io.netty.channel.ChannelHandlerContext;
19
20 import io.netty.util.concurrent.FastThreadLocal;
21 import org.jboss.marshalling.MarshallerFactory;
22 import org.jboss.marshalling.MarshallingConfiguration;
23 import org.jboss.marshalling.Unmarshaller;
24
25
26
27
28
29
30 public class ThreadLocalUnmarshallerProvider implements UnmarshallerProvider {
31 private final FastThreadLocal<Unmarshaller> unmarshallers = new FastThreadLocal<Unmarshaller>();
32
33 private final MarshallerFactory factory;
34 private final MarshallingConfiguration config;
35
36
37
38
39
40
41
42 public ThreadLocalUnmarshallerProvider(MarshallerFactory factory, MarshallingConfiguration config) {
43 this.factory = factory;
44 this.config = config;
45 }
46
47 @Override
48 public Unmarshaller getUnmarshaller(ChannelHandlerContext ctx) throws Exception {
49 Unmarshaller unmarshaller = unmarshallers.get();
50 if (unmarshaller == null) {
51 unmarshaller = factory.createUnmarshaller(config);
52 unmarshallers.set(unmarshaller);
53 }
54 return unmarshaller;
55 }
56
57 }