summaryrefslogtreecommitdiff
path: root/qpid/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/jms/impl/MessageProducerImpl.java
blob: 21629af7b19b8efddef3929518dd6010fc9139d3 (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
/*
 * 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.type.Binary;
import org.apache.qpid.amqp_1_0.type.UnsignedInteger;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import java.util.UUID;

public class MessageProducerImpl implements MessageProducer
{
    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;

    protected MessageProducerImpl(final Destination destination,
                               final SessionImpl session) throws JMSException
    {
        if(destination instanceof DestinationImpl)
        {
            _destination = (DestinationImpl) destination;
        }
        else if(destination != null)
        {
            // TODO - throw appropriate exception
        }
        _session = session;

        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;
        }
    }

    public boolean getDisableMessageID()
    {
        return _disableMessageID;
    }

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

    public boolean getDisableMessageTimestamp()
    {
        return _disableMessageTimestamp;
    }

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

    public int getDeliveryMode()
    {
        return _deliveryMode;
    }

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

    public int getPriority()
    {
        return _priority;
    }

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

    public long getTimeToLive()
    {
        return _timeToLive;
    }

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

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

    public void close() throws JMSException
    {
        //TODO
    }

    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
    {
        //TODO
        MessageImpl msg;
        if(message instanceof org.apache.qpid.amqp_1_0.jms.Message)
        {
            msg = (MessageImpl) message;
        }
        else
        {
            msg = convertMessage(message);
        }

        msg.setJMSDeliveryMode(deliveryMode);
        msg.setJMSPriority(priority);
        if(ttl != 0)
        {
            msg.setTtl(UnsignedInteger.valueOf(ttl));
        }
        else
        {
            msg.setTtl(null);
        }
        msg.setJMSDestination(_destination);
        if(!getDisableMessageTimestamp())
        {
            msg.setJMSTimestamp(System.currentTimeMillis());
        }
        if(!getDisableMessageID() && msg.getMessageId() == null)
        {
            msg.setMessageId(generateMessageId());
        }

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

        _sender.send(clientMessage);
    }

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

    private MessageImpl convertMessage(final Message message)
    {
        return null;  //TODO
    }

    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
    {
        //TODO
    }
}