]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | /* |
2 | * Licensed to the Apache Software Foundation (ASF) under one | |
3 | * or more contributor license agreements. See the NOTICE file | |
4 | * distributed with this work for additional information | |
5 | * regarding copyright ownership. The ASF licenses this file | |
6 | * to you under the Apache License, Version 2.0 (the | |
7 | * "License"); you may not use this file except in compliance | |
8 | * with the License. You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, | |
13 | * software distributed under the License is distributed on an | |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
15 | * KIND, either express or implied. See the License for the | |
16 | * specific language governing permissions and limitations | |
17 | * under the License. | |
18 | */ | |
19 | ||
20 | package org.apache.thrift.protocol; | |
21 | ||
22 | import org.apache.thrift.TException; | |
23 | ||
24 | /** | |
25 | * <code>TMultiplexedProtocol</code> is a protocol-independent concrete decorator | |
26 | * that allows a Thrift client to communicate with a multiplexing Thrift server, | |
27 | * by prepending the service name to the function name during function calls. | |
28 | * | |
29 | * <p>NOTE: THIS IS NOT USED BY SERVERS. On the server, use {@link org.apache.thrift.TMultiplexedProcessor TMultiplexedProcessor} to handle requests | |
30 | * from a multiplexing client. | |
31 | * | |
32 | * <p>This example uses a single socket transport to invoke two services: | |
33 | * | |
34 | * <pre> | |
35 | * {@code | |
36 | * TSocket transport = new TSocket("localhost", 9090); | |
37 | * transport.open(); | |
38 | * | |
39 | * TBinaryProtocol protocol = new TBinaryProtocol(transport); | |
40 | * | |
41 | * TMultiplexedProtocol mp = new TMultiplexedProtocol(protocol, "Calculator"); | |
42 | * Calculator.Client service = new Calculator.Client(mp); | |
43 | * | |
44 | * TMultiplexedProtocol mp2 = new TMultiplexedProtocol(protocol, "WeatherReport"); | |
45 | * WeatherReport.Client service2 = new WeatherReport.Client(mp2); | |
46 | * | |
47 | * System.out.println(service.add(2,2)); | |
48 | * System.out.println(service2.getTemperature()); | |
49 | * } | |
50 | * </pre> | |
51 | * | |
52 | * @see org.apache.thrift.protocol.TProtocolDecorator | |
53 | */ | |
54 | public class TMultiplexedProtocol extends TProtocolDecorator { | |
55 | ||
56 | /** Used to delimit the service name from the function name */ | |
57 | public static final String SEPARATOR = ":"; | |
58 | ||
59 | private final String SERVICE_NAME; | |
60 | ||
61 | /** | |
62 | * Wrap the specified protocol, allowing it to be used to communicate with a | |
63 | * multiplexing server. The <code>serviceName</code> is required as it is | |
64 | * prepended to the message header so that the multiplexing server can broker | |
65 | * the function call to the proper service. | |
66 | * | |
67 | * @param protocol Your communication protocol of choice, e.g. <code>TBinaryProtocol</code>. | |
68 | * @param serviceName The service name of the service communicating via this protocol. | |
69 | */ | |
70 | public TMultiplexedProtocol(TProtocol protocol, String serviceName) { | |
71 | super(protocol); | |
72 | SERVICE_NAME = serviceName; | |
73 | } | |
74 | ||
75 | /** | |
76 | * Prepends the service name to the function name, separated by TMultiplexedProtocol.SEPARATOR. | |
77 | * | |
78 | * @param tMessage The original message. | |
79 | * @throws TException Passed through from wrapped <code>TProtocol</code> instance. | |
80 | */ | |
81 | @Override | |
82 | public void writeMessageBegin(TMessage tMessage) throws TException { | |
83 | if (tMessage.type == TMessageType.CALL || tMessage.type == TMessageType.ONEWAY) { | |
84 | super.writeMessageBegin(new TMessage( | |
85 | SERVICE_NAME + SEPARATOR + tMessage.name, | |
86 | tMessage.type, | |
87 | tMessage.seqid | |
88 | )); | |
89 | } else { | |
90 | super.writeMessageBegin(tMessage); | |
91 | } | |
92 | } | |
93 | } |