LSSTApplications  11.0-13-gbb96280,12.1.rc1,12.1.rc1+1,12.1.rc1+2,12.1.rc1+5,12.1.rc1+8,12.1.rc1-1-g06d7636+1,12.1.rc1-1-g253890b+5,12.1.rc1-1-g3d31b68+7,12.1.rc1-1-g3db6b75+1,12.1.rc1-1-g5c1385a+3,12.1.rc1-1-g83b2247,12.1.rc1-1-g90cb4cf+6,12.1.rc1-1-g91da24b+3,12.1.rc1-2-g3521f8a,12.1.rc1-2-g39433dd+4,12.1.rc1-2-g486411b+2,12.1.rc1-2-g4c2be76,12.1.rc1-2-gc9c0491,12.1.rc1-2-gda2cd4f+6,12.1.rc1-3-g3391c73+2,12.1.rc1-3-g8c1bd6c+1,12.1.rc1-3-gcf4b6cb+2,12.1.rc1-4-g057223e+1,12.1.rc1-4-g19ed13b+2,12.1.rc1-4-g30492a7
LSSTDataManagementBasePackage
Receiver.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008-2016 AURA/LSST.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <https://www.lsstcorp.org/LegalNotices/>.
23  */
24 
33 #include <iomanip>
34 
36 
37 #include "lsst/pex/exceptions.h"
38 
41 
42 #include <activemq/core/ActiveMQConnectionFactory.h>
43 #include <activemq/exceptions/ActiveMQException.h>
44 
45 namespace pexExceptions = lsst::pex::exceptions;
46 
47 namespace activemqCore = activemq::core;
48 
49 namespace lsst {
50 namespace ctrl {
51 namespace events {
52 
55 }
56 
59 void Receiver::init(const std::string& hostName, const std::string& destinationName, const std::string& selector, bool createQueue, int hostPort) {
60 
61  _connection = NULL;
62  _session = NULL;
63  _destination = NULL;
64  _consumer = NULL;
65  _destinationName = destinationName;
66  _selector = selector;
67 
68  try {
69  std::stringstream ss;
70 
71  ss << hostPort;
72 
73  std::string jmsURL = "tcp://"+hostName+":"+ss.str()+"?wireFormat=openwire";
74 
75  activemqCore::ActiveMQConnectionFactory* connectionFactory =
76  new activemqCore::ActiveMQConnectionFactory( jmsURL );
77 
78  _connection = 0;
79  try {
80  _connection = connectionFactory->createConnection();
81  _connection->start();
82  delete connectionFactory;
83  }
84  catch (cms::CMSException& e) {
85  delete connectionFactory;
86  std::string msg("Failed to connect to broker: ");
87  msg += e.getMessage();
88  msg += " (is broker running?)";
89  throw LSST_EXCEPT(pexExceptions::RuntimeError, msg);
90  }
91 
92  _session = _connection->createSession( cms::Session::AUTO_ACKNOWLEDGE );
93 
94  if (createQueue) {
95  _destination = _session->createQueue( destinationName );
96  } else {
97  _destination = _session->createTopic( destinationName );
98  }
99 
100  if (_selector == "")
101  _consumer = _session->createConsumer( _destination );
102  else
103  _consumer = _session->createConsumer( _destination, selector );
104 
105  } catch ( cms::CMSException& e ) {
106  throw LSST_EXCEPT(pexExceptions::RuntimeError, std::string("Trouble creating Receiver: ") + e.getMessage());
107  }
108 }
109 
110 PTR(Event) Receiver::receiveEvent() {
111  return receiveEvent(infiniteTimeout);
112 }
113 
114 PTR(Event) Receiver::receiveEvent(long timeout) {
115 
116  cms::TextMessage* textMessage;
117  try {
118  cms::Message* msg = _consumer->receive(timeout);
119  if (msg == NULL) return NULL;
120  textMessage = dynamic_cast<cms::TextMessage* >(msg);
121  if (textMessage == NULL)
122  throw LSST_EXCEPT(pexExceptions::RuntimeError, "Unexpected JMS Message type");
123  } catch (activemq::exceptions::ActiveMQException& e) {
124  throw LSST_EXCEPT(pexExceptions::RuntimeError, e.getMessage());
125  }
126 
127  PTR(Event) event(EventFactory().createEvent(textMessage));
128  delete textMessage;
129 
130  return event;
131 }
132 
134  return _destinationName;
135 }
136 
137 std::string Receiver::getSelector() {
138  return _selector;
139 }
140 
142 
143  // Destroy resources.
144  try {
145  if( _destination != NULL )
146  delete _destination;
147  } catch ( cms::CMSException& e ) {
148  e.printStackTrace();
149  }
150  _destination = NULL;
151 
152  try {
153  if( _consumer != NULL )
154  delete _consumer;
155  } catch ( cms::CMSException& e ) {
156  e.printStackTrace();
157  }
158  _consumer = NULL;
159 
160  // Close open resources.
161  try {
162  if( _session != NULL )
163  _session->close();
164  } catch ( cms::CMSException& e ) {
165  e.printStackTrace();
166  }
167  try {
168  if( _connection != NULL )
169  _connection->close();
170  } catch ( cms::CMSException& e ) {
171  e.printStackTrace();
172  }
173 
174  try {
175  if( _session != NULL )
176  delete _session;
177  } catch ( cms::CMSException& e ) {
178  e.printStackTrace();
179  }
180  _session = NULL;
181 
182  try {
183  if( _connection != NULL )
184  delete _connection;
185  } catch ( cms::CMSException& e ) {
186  e.printStackTrace();
187  }
188  _connection = NULL;
189 }
190 
191 }}}
void init(const std::string &hostName, const std::string &destinationName, const std::string &selector, bool createQueue, int hostPort)
Definition: Receiver.cc:59
Singleton use to make sure the events library is initialized.
Definition: EventLibrary.h:49
std::string getDestinationName()
get destination name
Definition: Receiver.cc:133
create LSST Events from JMS Messages
Definition: EventFactory.h:57
cms::MessageConsumer * _consumer
Definition: Receiver.h:120
cms::Destination * _destination
Definition: Receiver.h:117
std::string getSelector()
get selector name
Definition: Receiver.cc:137
cms::Connection * _connection
Definition: Receiver.h:114
std::string _destinationName
Definition: Receiver.h:104
virtual ~Receiver()
destructor
Definition: Receiver.cc:141
defines the EventFactory class
defines the Receiver class
defines the EventLibrary class
Receive events from the event bus.
Definition: Receiver.h:61
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
Representation of an LSST Event.
Definition: Event.h:61
#define PTR(...)
Definition: base.h:41
static void initializeLibrary()
initialize the ActiveMQ library, but only do it once.
Definition: EventLibrary.cc:41
Include files required for standard LSST Exception handling.
cms::Session * _session
Definition: Receiver.h:107