Skip to content

Commit 88cc2d3

Browse files
author
翊名
committed
feat(OMS) polish MessagingAccessPoint builder
1 parent 4475cbb commit 88cc2d3

12 files changed

Lines changed: 162 additions & 254 deletions

File tree

openmessaging-api-samples/src/main/java/io/openmessaging/samples/consumer/PullConsumerApp.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@ public class PullConsumerApp {
3030
public static void main(String[] args) {
3131
//Load and start the vendor implementation from a specific OMS driver URL.
3232
final MessagingAccessPoint messagingAccessPoint =
33-
OMS.getMessagingAccessPoint("oms:rocketmq://alice@rocketmq.apache.org/us-east");
33+
OMS.builder()
34+
.endpoint("http://mq-instance-xxx-1234567890-test:8080")
35+
.region("Shenzhen")
36+
.driver("rocketmq")
37+
.build();
38+
3439
Properties properties = new Properties();
3540
//Start a PullConsumer to receive messages from the specific queue.
3641
final PullConsumer consumer = messagingAccessPoint.createPullConsumer(properties);

openmessaging-api-samples/src/main/java/io/openmessaging/samples/consumer/PushConsumerApp.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@ public class PushConsumerApp {
3030
public static void main(String[] args) {
3131
//Load and start the vendor implementation from a specific OMS driver URL.
3232
final MessagingAccessPoint messagingAccessPoint =
33-
OMS.getMessagingAccessPoint("oms:rocketmq://localhost:9876");
33+
OMS.builder()
34+
.region("Shenzhen")
35+
.endpoint("127.0.0.1:9876")
36+
.driver("rocketmq")
37+
.withCredentials(new Properties())
38+
.build();
3439

3540
Properties properties = new Properties();
3641
final Consumer consumer = messagingAccessPoint.createConsumer(properties);

openmessaging-api-samples/src/main/java/io/openmessaging/samples/producer/ProducerApp.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@
2929
public class ProducerApp {
3030
public static void main(String[] args) {
3131
final MessagingAccessPoint messagingAccessPoint =
32-
OMS.getMessagingAccessPoint("oms:rocketmq://alice@rocketmq.apache.org");
32+
OMS.builder()
33+
.region("shanghai,shenzhen")
34+
.endpoint("127.0.0.1:9876")
35+
.driver("rocketmq")
36+
.withCredentials(new Properties())
37+
.build();
3338

3439
final Producer producer = messagingAccessPoint.createProducer(new Properties());
3540
producer.start();

openmessaging-api-samples/src/main/java/io/openmessaging/samples/producer/TransactionProducerApp.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@
3030
public class TransactionProducerApp {
3131
public static void main(String[] args) {
3232
final MessagingAccessPoint messagingAccessPoint =
33-
OMS.getMessagingAccessPoint("oms:rocketmq://alice@rocketmq.apache.org/us-east");
33+
OMS.builder()
34+
.region("Shenzhen")
35+
.endpoint("127.0.0.1:9876")
36+
.driver("rocketmq")
37+
.withCredentials(new Properties())
38+
.build();
3439

3540
final TransactionProducer producer = messagingAccessPoint.createTransactionProducer(new Properties(), new LocalTransactionChecker() {
3641
@Override

openmessaging-api/src/main/java/io/openmessaging/api/MessagingAccessPoint.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,10 @@ public interface MessagingAccessPoint {
5454
* <p>
5555
* There are some standard attributes defined by OMS for {@code MessagingAccessPoint}:
5656
* <ul>
57-
* <li> {@link OMSBuiltinKeys#ACCESS_POINTS}, the specified access points.
57+
* <li> {@link OMSBuiltinKeys#ENDPOINT}, the specified access points.
5858
* <li> {@link OMSBuiltinKeys#DRIVER_IMPL}, the fully qualified class name of the specified MessagingAccessPoint's
5959
* implementation, the default value is {@literal io.openmessaging.<driver_type>.MessagingAccessPointImpl}.
6060
* <li> {@link OMSBuiltinKeys#REGION}, the region the resources reside in.
61-
* <li> {@link OMSBuiltinKeys#ACCOUNT_ID}, the ID of the specific account system that owns the resource.
6261
* </ul>
6362
*
6463
* @return the attributes

openmessaging-api/src/main/java/io/openmessaging/api/OMS.java

Lines changed: 102 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,57 +17,133 @@
1717

1818
package io.openmessaging.api;
1919

20-
import io.openmessaging.api.exception.OMSRuntimeException;
2120
import io.openmessaging.api.internal.MessagingAccessPointAdapter;
2221
import java.io.IOException;
2322
import java.io.InputStream;
23+
import java.util.Map;
2424
import java.util.Properties;
25+
import java.util.Set;
2526

2627
/**
28+
* <p>
2729
* The oms class provides some static methods to create a {@code MessagingAccessPoint} from the specified OMS driver url
2830
* and some useful util methods.
29-
* <p>
30-
* The complete OMS driver URL syntax is:
31-
* <p>
32-
* {@literal oms:<driver_type>://[account_id@]host1[:port1][,host2[:port2],...[,hostN[:portN]]]/<region>}
33-
* <p>
34-
* The first part of the URL specifies which OMS implementation is to be used, rocketmq is an optional driver type.
31+
* </p>
32+
*
3533
* <p>
3634
* The brackets indicate that the extra access points are optional, and a correct OMS driver url needs at least one
3735
* access point, which consists of hostname and port, like localhost:8081.
36+
* </p>
3837
*
39-
* @version OMS 1.1.0
40-
* @since OMS 1.1.0
38+
* @version OMS 1.2.0
39+
* @since OMS 1.1.0
4140
*/
4241
public final class OMS {
42+
43+
private final Properties properties = new Properties();
44+
45+
public static OMS builder() {
46+
return new OMS();
47+
}
48+
4349
/**
44-
* Returns a {@code MessagingAccessPoint} instance from the specified OMS driver url.
50+
* Set the endpoint provided by messaging vendor.
4551
*
46-
* @param url the specified OMS driver url
47-
* @return a {@code MessagingAccessPoint} instance
48-
* @throws OMSRuntimeException if the factory fails to create a {@code MessagingAccessPoint} due to some driver url
49-
* some syntax error or internal error.
52+
* @param endpoint
53+
* @return
5054
*/
51-
public static MessagingAccessPoint getMessagingAccessPoint(String url) {
52-
return getMessagingAccessPoint(url, new Properties());
55+
public OMS endpoint(String endpoint) {
56+
this.properties.put(OMSBuiltinKeys.ENDPOINT, endpoint);
57+
return this;
5358
}
5459

5560
/**
56-
* Returns a {@code MessagingAccessPoint} instance from the specified OMS driver url with some preset attributes,
57-
* which will be passed to MessagingAccessPoint's implementation class as a unique constructor parameter.
61+
* Set the region provided by messaging vendor.
5862
*
59-
* There are some standard attributes defined by OMS for this method, the same as {@link
60-
* MessagingAccessPoint#attributes()} ()}
63+
* @param region
64+
* @return
65+
*/
66+
public OMS region(String region) {
67+
this.properties.put(OMSBuiltinKeys.REGION, region);
68+
return this;
69+
}
70+
71+
/**
72+
* <p>
73+
* Set the the driver type of the specified MessagingAccessPoint's * implementation, the default value is {@literal
74+
* io.openmessaging.<driver_type>.MessagingAccessPointImpl}.
75+
* </p>
76+
*
77+
* <p>
78+
* But if the {@link OMS#driverImpl(String)} attribute was set, this attribute will be ignored.
79+
* </p>
6180
*
62-
* @param url the specified OMS driver url
63-
* @return a {@code MessagingAccessPoint} instance
64-
* @throws OMSRuntimeException if the factory fails to create a {@code MessagingAccessPoint} due to some driver url
65-
* some syntax error or internal error.
81+
* @param driver
82+
* @return
6683
*/
67-
public static MessagingAccessPoint getMessagingAccessPoint(String url, Properties attributes) {
68-
return MessagingAccessPointAdapter.getMessagingAccessPoint(url, attributes);
84+
public OMS driver(String driver) {
85+
this.properties.put(OMSBuiltinKeys.DRIVER, driver);
86+
return this;
6987
}
7088

89+
/**
90+
* <p>
91+
* Set the the fully qualified class name of the specified MessagingAccessPoint's * implementation, the default
92+
* value is {@literal io.openmessaging.<driver_type>.MessagingAccessPointImpl}.
93+
* </p>
94+
*
95+
* <p>
96+
* If this attribute was set, {@link OMS#driver(String)} will be ignored.
97+
* </p>
98+
*
99+
* @param driverImpl
100+
* @return
101+
*/
102+
public OMS driverImpl(String driverImpl) {
103+
this.properties.put(OMSBuiltinKeys.DRIVER_IMPL, driverImpl);
104+
return this;
105+
}
106+
107+
/**
108+
* <p>
109+
* Set credentials used by the client.
110+
* </p>
111+
*
112+
* @param credentials provided by vendors.
113+
* @return
114+
*/
115+
public OMS withCredentials(Properties credentials) {
116+
if (credentials.getProperty(OMSBuiltinKeys.ACCESS_KEY) != null) {
117+
this.properties.put(OMSBuiltinKeys.ACCESS_KEY, credentials.getProperty(OMSBuiltinKeys.ACCESS_KEY));
118+
}
119+
if (credentials.getProperty(OMSBuiltinKeys.SECRET_KEY) != null) {
120+
this.properties.put(OMSBuiltinKeys.SECRET_KEY, credentials.getProperty(OMSBuiltinKeys.SECRET_KEY));
121+
}
122+
if (credentials.getProperty(OMSBuiltinKeys.SECURITY_TOKEN) != null) {
123+
this.properties.put(OMSBuiltinKeys.SECURITY_TOKEN, credentials.getProperty(OMSBuiltinKeys.SECURITY_TOKEN));
124+
}
125+
return this;
126+
}
127+
128+
public MessagingAccessPoint build() {
129+
return MessagingAccessPointAdapter.getMessagingAccessPoint(this.properties);
130+
}
131+
132+
/**
133+
* Set extra custom configs.
134+
*
135+
* @param config extra configs
136+
* @return
137+
*/
138+
public MessagingAccessPoint build(Properties config) {
139+
Set<Map.Entry<Object, Object>> entrySet = config.entrySet();
140+
for (Map.Entry<Object, Object> entry : entrySet) {
141+
if (!this.properties.containsKey(entry.getKey())) {
142+
this.properties.put(entry.getKey(), entry.getValue());
143+
}
144+
}
145+
return this.build(properties);
146+
}
71147

72148
/**
73149
* The version format is X.Y.Z (Major.Minor.Patch), a pre-release version may be denoted by appending a hyphen and a

openmessaging-api/src/main/java/io/openmessaging/api/OMSBuiltinKeys.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,34 +27,37 @@ public interface OMSBuiltinKeys {
2727
/**
2828
* The {@code DRIVER_IMPL} key represents the vendor implementation entry of {@link MessagingAccessPoint}.
2929
*/
30-
String DRIVER_IMPL = "DRIVER_IMPL";
31-
32-
/**
33-
* The {@code ACCESS_POINTS} key shows the specified access points in OMS driver schema.
34-
*
35-
* @see <a href="https://github.com/openmessaging/specification/blob/master/oms_access_point_schema.md">Access Point
36-
* Schema</a>
37-
*/
38-
String ACCESS_POINTS = "ACCESS_POINTS";
30+
String DRIVER_IMPL = "driverImpl";
3931

4032
/**
4133
* The {@code ACCESS_KEY} key shows the specified access key in OMS driver schema.
4234
*/
43-
String ACCESS_KEY = "AccessKey";
35+
String ACCESS_KEY = "accessKey";
4436

4537
/**
4638
* The {@code SECRET_KEY} key shows the specified secret key in OMS attribute.
4739
*/
48-
String SECRET_KEY = "SecretKey";
40+
String SECRET_KEY = "secretKey";
4941

5042
/**
5143
* The {@code SECURITY_TOKEN} key shows the specified security token in OMS attribute.
5244
*/
53-
String SECURITY_TOKEN = "SecurityToken";
45+
String SECURITY_TOKEN = "securityToken";
5446

5547
/**
5648
* The {@code REGION} key shows the specified region in OMS driver schema.
5749
*/
58-
String REGION = "REGION";
50+
String REGION = "region";
51+
52+
/**
53+
* The {@code ENDPOINT} key shows the specified host in OMS attribute.
54+
*/
55+
String ENDPOINT = "endpoint";
56+
57+
/**
58+
* The {@code DRIVER} key represents the vendor type of {@link MessagingAccessPoint}, but if {@code DRIVER_IMPL} is
59+
* not empty, this {@code DRIVER} value will be ignored.
60+
*/
61+
String DRIVER = "driver";
5962

6063
}

openmessaging-api/src/main/java/io/openmessaging/api/TopicPartition.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package io.openmessaging.api;
1818

1919
/**
20-
* Used for describe a topic and
20+
* Used for describe a topic
2121
* @version OMS 1.2.0
2222
* @since OMS 1.2.0
2323
*/
@@ -80,4 +80,11 @@ public boolean equals(Object obj) {
8080
}
8181
return true;
8282
}
83+
84+
@Override public String toString() {
85+
return "TopicPartition{" +
86+
"topic='" + topic + '\'' +
87+
", partition='" + partition + '\'' +
88+
'}';
89+
}
8390
}

0 commit comments

Comments
 (0)