summaryrefslogtreecommitdiff
path: root/qpid/java/client/example/src/main/java/org/apache/qpid/example/publisher/Publisher.java
blob: b5f44557a435061452427788d1ae492b8217986e (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
/*
 * 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.example.publisher;

import org.apache.qpid.client.AMQConnectionFactory;

import javax.jms.*;

import javax.naming.InitialContext;

import org.apache.qpid.example.shared.InitialContextHelper;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;

public class Publisher
{
    private static final Logger _log = LoggerFactory.getLogger(Publisher.class);

    protected InitialContextHelper _contextHelper;

    protected Connection _connection;

    protected Session _session;

    protected MessageProducer _producer;

    protected String _destinationDir;

    protected String _name = "Publisher";

    protected Destination _destination;

    protected static final String _defaultDestinationDir = "/tmp";

    /**
     * Creates a Publisher instance using properties from example.properties
     * See InitialContextHelper for details of how context etc created
     */
    public Publisher()
    {
        try
        {
            //get an initial context from default properties
            _contextHelper = new InitialContextHelper(null);
            InitialContext ctx = _contextHelper.getInitialContext();

            //then create a connection using the AMQConnectionFactory
            AMQConnectionFactory cf = (AMQConnectionFactory) ctx.lookup("local");
            _connection = cf.createConnection();

            _connection.setExceptionListener(new ExceptionListener()
            {
                public void onException(JMSException jmse)
                {
                    // The connection may have broken invoke reconnect code if available.
                    // The connection may have broken invoke reconnect code if available.
                    System.err.println("ExceptionListener caught: " + jmse);
                    //System.exit(0);
                }
            });

            //create a transactional session
            _session = _connection.createSession(true, Session.AUTO_ACKNOWLEDGE);

            //lookup the example queue and use it
            //Queue is non-exclusive and not deleted when last consumer detaches
            _destination = (Queue) ctx.lookup("MyQueue");

            //create a message producer
            _producer = _session.createProducer(_destination);

            //set destination dir for files that have been processed
            _destinationDir = _defaultDestinationDir;

            _connection.start();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            _log.error("Exception", e);
        }
    }

    /**
     * Creates and sends the number of messages specified in the param
     */
    public void sendMessage(int numMessages)
    {
        try
        {
            TextMessage txtMessage = _session.createTextMessage("msg");
            for (int i=0;i<numMessages;i++)
            {
                sendMessage(txtMessage);
                _log.info("Sent: " + i);
            }
        }
        catch (JMSException j)
        {
            _log.error("Exception in sendMessage" + j);
        }


    }

    /**
     * Publishes a non-persistent message using transacted session
     * Note that persistent is the default mode for send - so need to specify for transient
     */
    public boolean sendMessage(Message message)
    {
        try
        {
            //Send message via our producer which is not persistent
            _producer.send(message, DeliveryMode.PERSISTENT, _producer.getPriority(), _producer.getTimeToLive());

            //commit the message send and close the transaction
            _session.commit();

        }
        catch (JMSException e)
        {
            //Have to assume our commit failed and rollback here
            try
            {
                _session.rollback();
                _log.error("JMSException", e);
                e.printStackTrace();
                return false;
            }
            catch (JMSException j)
            {
                _log.error("Unable to rollback publish transaction ",e);
                return false;
            }
        }

        //_log.info(_name + " finished sending message: " + message);
        return true;
    }

    /**
     * Cleanup resources before exit
     */
    public void cleanup()
    {
        try
        {
            if (_connection != null)
            {
                _connection.stop();
                _connection.close();
            }
            _connection = null;
            _producer = null;
        }
        catch(Exception e)
        {
            _log.error("Error trying to cleanup publisher " + e);
            System.exit(1);
        }
    }

    /**
     * Exposes session
     * @return  Session
     */
    public Session getSession()
    {
        return _session;
    }

    public String getDestinationDir()
    {
        return _destinationDir;
    }

    public void setDestinationDir(String destinationDir)
    {
        _destinationDir = destinationDir;
    }

    public String getName()
    {
        return _name;
    }

    public void setName(String _name) {
        this._name = _name;
    }
}