1 /*
2  * hunt-amqp-client: AMQP Client Library for D Programming Language. Support for RabbitMQ and other AMQP Server.
3  *
4  * Copyright (C) 2018-2019 HuntLabs
5  *
6  * Website: https://www.huntlabs.net
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 
12 module hunt.amqp.client.AmqpClient;
13 
14 import hunt.amqp.client.impl.AmqpClientImpl;
15 import hunt.amqp.client.AmqpClientOptions;
16 import hunt.amqp.client.AmqpSender;
17 import hunt.amqp.Handler;
18 import hunt.amqp.client.AmqpConnection;
19 import hunt.amqp.client.AmqpReceiver;
20 import hunt.amqp.client.AmqpReceiverOptions;
21 import hunt.amqp.client.AmqpSenderOptions;
22 
23 import hunt.concurrency.Future;
24 import hunt.Object;
25 // import hunt.Functions;
26 
27 // alias AmqpHandler = Action1;
28 
29 
30 /**
31  * AMQP Client entry point.
32  * Use this interface to create an instance of {@link AmqpClient} and connect to a broker and server.
33  */
34 interface AmqpClient {
35 
36     /**
37      * Creates a new instance of {@link AmqpClient} using an internal Vert.x instance (with default configuration) and
38      * the given AMQP client configuration. Note that the created Vert.x instance will be closed when the client is
39      * closed.
40      *
41      * @param options the AMQP client options, may be {@code null} falling back to the default configuration
42      * @return the created instances.
43      */
44     static AmqpClient create(AmqpClientOptions options) {
45         return new AmqpClientImpl(options, true);
46     }
47 
48     /**
49      * Creates a new instance of {@link AmqpClient} with the given Vert.x instance and the given options.
50      *
51      * @param vertx   the vert.x instance, must not be {@code null}
52      * @param options the AMQP options, may be @{code null} falling back to the default configuration
53      * @return the AMQP client instance
54      */
55     //static AmqpClient create(Vertx vertx, AmqpClientOptions options) {
56     //  return new AmqpClientImpl(Objects.requireNonNull(vertx), options, false);
57     //}
58 
59     /**
60      * Connects to the AMQP broker or router. The location is specified in the {@link AmqpClientOptions} as well as the
61      * potential credential required.
62      *
63      * @param connectionHandler handler that will process the result, giving either the connection or failure cause. Must
64      *                          not be {@code null}.
65      */
66     AmqpClient connect(AsyncResultHandler!AmqpConnection connectionHandler);
67 
68     /**
69      * Like {@link #connect(Handler)} but returns a {@code Future} of the asynchronous result
70      */
71     Future!AmqpConnection connectAsync();
72 
73     // ditto
74     AmqpConnection connect();
75 
76     /**
77      * Closes the client.
78      * The client must always be closed once not needed anymore.
79      *
80      * @param closeHandler the close handler notified when the operation completes. It can be {@code null}.
81      */
82     void close(AsyncResultHandler!Void closeHandler);
83 
84     /**
85      * Like {@link #close(Handler)} but returns a {@code Future} of the asynchronous result
86      */
87  // Future<Void> close();
88 
89     /**
90      * Creates a receiver used to consume messages from the given address. The receiver has no handler and won't
91      * start receiving messages until a handler is explicitly configured. This method avoids having to connect explicitly.
92      * You can retrieve the connection using {@link AmqpReceiver#connection()}.
93      *
94      * @param address           The source address to attach the consumer to, must not be {@code null}
95      * @param completionHandler the handler called with the receiver. The receiver has been opened.
96      * @return the client.
97      */
98     AmqpClient createReceiver(string address, Handler!AmqpReceiver completionHandler);
99 
100     /**
101      * Like {@link #createReceiver(String, Handler)} but returns a {@code Future} of the asynchronous result
102      */
103  // Future<AmqpReceiver> createReceiver(String address);
104 
105     /**
106      * Creates a receiver used to consumer messages from the given address.  This method avoids having to connect
107      * explicitly. You can retrieve the connection using {@link AmqpReceiver#connection()}.
108      *
109      * @param address           The source address to attach the consumer to.
110      * @param receiverOptions   The options for this receiver.
111      * @param completionHandler The handler called with the receiver, once opened. Note that the {@code messageHandler}
112      *                          can be called before the {@code completionHandler} if messages are awaiting delivery.
113      * @return the connection.
114      */
115     AmqpClient createReceiver(string address, AmqpReceiverOptions receiverOptions,
116         Handler!AmqpReceiver completionHandler);
117 
118     /**
119      * Like {@link #createReceiver(String, AmqpReceiverOptions, Handler)} but returns a {@code Future} of the asynchronous result
120      */
121  // Future<AmqpReceiver> createReceiver(String address, AmqpReceiverOptions receiverOptions);
122 
123     /**
124      * Creates a sender used to send messages to the given address. The address must be set.
125      *
126      * @param address           The target address to attach to, must not be {@code null}
127      * @param completionHandler The handler called with the sender, once opened
128      * @return the client.
129      */
130     AmqpClient createSender(string address, Handler!AmqpSender completionHandler);
131 
132     /**
133      * Like {@link #createSender(String, Handler)} but returns a {@code Future} of the asynchronous result
134      */
135     //Future<AmqpSender> createSender(String address);
136 
137     /**
138      * Creates a sender used to send messages to the given address. The address must be set.
139      *
140      * @param address           The target address to attach to, must not be {@code null}
141      * @param options The options for this sender.
142      * @param completionHandler The handler called with the sender, once opened
143      * @return the client.
144      */
145     AmqpClient createSender(string address, AmqpSenderOptions options,
146                                                     Handler!AmqpSender completionHandler);
147 
148     /**
149      * Like {@link #createSender(String, AmqpSenderOptions, Handler)} but returns a {@code Future} of the asynchronous result
150      */
151     //Future<AmqpSender> createSender(String address, AmqpSenderOptions options);
152 
153 }