summaryrefslogtreecommitdiff
path: root/java/amqp-1-0-client-jms/src/main/java/org/apache/qpid/amqp_1_0/jms/impl/MessageProducerImpl.java
blob: 5bb8845eb77256463c76e15b88dd221b3f7c05fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.qpid.amqp_1_0.jms.impl;

import org.apache.qpid.amqp_1_0.client.Sender;
import org.apache.qpid.amqp_1_0.jms.MessageProducer;
import org.apache.qpid.amqp_1_0.jms.Queue;
import org.apache.qpid.amqp_1_0.jms.QueueSender;
import org.apache.qpid.amqp_1_0.jms.TemporaryDestination;
import org.apache.qpid.amqp_1_0.jms.TopicPublisher;
import org.apache.qpid.amqp_1_0.type.Binary;
import org.apache.qpid.amqp_1_0.type.UnsignedInteger;

import javax.jms.*;
import javax.jms.IllegalStateException;
import java.util.UUID;

public class MessageProducerImpl implements MessageProducer, QueueSender, TopicPublisher
{
    private boolean _disableMessageID;
    private boolean _disableMessageTimestamp;
    private int _deliveryMode = Message.DEFAULT_DELIVERY_MODE;
    private int _priority = Message.DEFAULT_PRIORITY;
    private long _timeToLive;

    private DestinationImpl _destination;
    private SessionImpl _session;
    private Sender _sender;
    private boolean _closed;

    protected MessageProducerImpl(final Destination destination,
                               final SessionImpl session) throws JMSException
    {
        if(destination instanceof DestinationImpl)
        {
            _destination = (DestinationImpl) destination;
        }
        else if(destination != null)
        {
            throw new InvalidDestinationException("Invalid Destination Class" + destination.getClass().getName());
        }
        _session = session;

        if(_destination != null)
        {
            try
            {
                _sender = _session.getClientSession().createSender(_destination.getAddress());
            }
            catch (Sender.SenderCreationException e)
            {
                // TODO - refine exception
                JMSException jmsEx = new JMSException(e.getMessage());
                jmsEx.initCause(e);
                jmsEx.setLinkedException(e);
                throw jmsEx;
            }
        }
    }

    private void checkClosed() throws IllegalStateException
    {
        if(_closed)
        {
            throw new javax.jms.IllegalStateException("Producer closed");
        }
    }

    public boolean getDisableMessageID() throws IllegalStateException
    {
        checkClosed();
        return _disableMessageID;
    }

    public void setDisableMessageID(final boolean disableMessageID) throws IllegalStateException
    {
        checkClosed();
        _disableMessageID = disableMessageID;
    }

    public boolean getDisableMessageTimestamp() throws IllegalStateException
    {
        checkClosed();
        return _disableMessageTimestamp;
    }

    public void setDisableMessageTimestamp(final boolean disableMessageTimestamp) throws IllegalStateException
    {
        checkClosed();
        _disableMessageTimestamp = disableMessageTimestamp;
    }

    public int getDeliveryMode() throws IllegalStateException
    {
        checkClosed();
        return _deliveryMode;
    }

    public void setDeliveryMode(final int deliveryMode) throws IllegalStateException
    {
        checkClosed();
        _deliveryMode = deliveryMode;
    }

    public int getPriority() throws IllegalStateException
    {
        checkClosed();
        return _priority;
    }

    public void setPriority(final int priority) throws IllegalStateException
    {
        checkClosed();
        _priority = priority;
    }

    public long getTimeToLive() throws IllegalStateException
    {
        checkClosed();
        return _timeToLive;
    }

    public void setTimeToLive(final long timeToLive) throws IllegalStateException
    {
        checkClosed();
        _timeToLive = timeToLive;
    }

    public DestinationImpl getDestination() throws JMSException
    {
        checkClosed();
        return _destination;
    }

    public void close() throws JMSException
    {
        try
        {
            if(!_closed)
            {
                _closed = true;
                if(_sender != null)
                {
                    _sender.close();
                }
            }

        }
        catch (Sender.SenderClosingException e)
        {
            final JMSException jmsException = new JMSException("error closing");
            jmsException.setLinkedException(e);
            throw jmsException;
        }
    }

    public void send(final Message message) throws JMSException
    {
        send(message, getDeliveryMode(), getPriority(), getTimeToLive());
    }

    public void send(final Message message, final int deliveryMode, final int priority, final long ttl) throws JMSException
    {
        if(_sender == null)
        {
            throw new UnsupportedOperationException("No Destination provided");
        }
        if(_destination instanceof TemporaryDestination && ((TemporaryDestination)_destination).isDeleted())
        {
            throw new IllegalStateException("Destination is deleted");
        }


        //TODO
        MessageImpl msg;
        if(message instanceof org.apache.qpid.amqp_1_0.jms.Message)
        {
            msg = (MessageImpl) message;
        }
        else
        {
            msg = _session.convertMessage(message);
        }



        msg.setJMSDeliveryMode(deliveryMode);
        msg.setJMSPriority(priority);

        msg.setJMSDestination(_destination);

        long timestamp = 0l;

        if(!getDisableMessageTimestamp() || ttl != 0)
        {
            timestamp = System.currentTimeMillis();
            msg.setJMSTimestamp(timestamp);

        }
        if(ttl != 0)
        {
            msg.setTtl(UnsignedInteger.valueOf(ttl));
        }
        else
        {
            msg.setTtl(null);
        }

        if(!getDisableMessageID() && msg.getMessageId() == null)
        {
            final Binary messageId = generateMessageId();
            msg.setMessageId(messageId);

        }

        if(message != msg)
        {
            message.setJMSTimestamp(msg.getJMSTimestamp());
            message.setJMSMessageID(msg.getJMSMessageID());
            message.setJMSDeliveryMode(msg.getJMSDeliveryMode());
            message.setJMSPriority(msg.getJMSPriority());
            message.setJMSExpiration(msg.getJMSExpiration());
        }


        final org.apache.qpid.amqp_1_0.client.Message clientMessage = new org.apache.qpid.amqp_1_0.client.Message(msg.getSections());

        _sender.send(clientMessage, _session.getTxn());

        if(getDestination() != null)
        {
            message.setJMSDestination(getDestination());
        }
    }

    public void send(final javax.jms.Queue queue, final Message message) throws JMSException
    {
        send((Destination)queue, message);
    }

    public void send(final javax.jms.Queue queue, final Message message, final int deliveryMode, final int priority, final long ttl)
            throws JMSException
    {
        send((Destination)queue, message, deliveryMode, priority, ttl);
    }

    private Binary generateMessageId()
    {
        UUID uuid = UUID.randomUUID();
        return new Binary(uuid.toString().getBytes());
    }

    public void send(final Destination destination, final Message message) throws JMSException
    {
        send(destination, message, getDeliveryMode(), getPriority(), getTimeToLive());
    }

    public void send(final Destination destination, final Message message, final int deliveryMode, final int priority, final long ttl)
            throws JMSException
    {

        checkClosed();
        if(destination == null)
        {
            send(message, deliveryMode, priority, ttl);
        }
        else
        {
            if(_destination != null)
            {
                throw new UnsupportedOperationException("Cannot use explicit destination pon non-anonymous producer");
            }
            else if(!(destination instanceof DestinationImpl))
            {
                throw new InvalidDestinationException("Invalid Destination Class" + destination.getClass().getName());
            }
            else if(destination instanceof TemporaryDestination && ((TemporaryDestination)destination).isDeleted())
            {
                throw new IllegalStateException("Destination has been deleted");
            }
            try
            {
                _destination = (DestinationImpl) destination;
                _sender = _session.getClientSession().createSender(_destination.getAddress());

                send(message, deliveryMode, priority, ttl);

                _sender.close();



            }
            catch (Sender.SenderCreationException e)
            {
                // TODO - refine exception
                JMSException jmsEx = new JMSException(e.getMessage());
                jmsEx.initCause(e);
                jmsEx.setLinkedException(e);
                throw jmsEx;
            }
            catch (Sender.SenderClosingException e)
            {
                JMSException jmsEx = new JMSException(e.getMessage());
                jmsEx.initCause(e);
                jmsEx.setLinkedException(e);
                throw jmsEx;
            }
            finally
            {
                _sender = null;
                _destination = null;
            }
        }
    }

    public QueueImpl getQueue() throws JMSException
    {
        return (QueueImpl) getDestination();
    }

    public TopicImpl getTopic() throws JMSException
    {
        return (TopicImpl) getDestination();
    }

    public void publish(final Message message) throws JMSException
    {
        send(message);
    }

    public void publish(final Message message, final int deliveryMode, final int priority, final long ttl) throws JMSException
    {
        send(message, deliveryMode, priority, ttl);
    }

    public void publish(final Topic topic, final Message message) throws JMSException
    {
        send(topic, message);
    }

    public void publish(final Topic topic, final Message message, final int deliveryMode, final int priority, final long ttl)
            throws JMSException
    {
        send(topic, message, deliveryMode, priority, ttl);
    }
}