|
| 1 | +# encoding: utf-8 |
| 2 | +require "logstash/inputs/base" |
| 3 | +require "logstash/inputs/threadable" |
| 4 | +require "logstash/namespace" |
| 5 | + |
| 6 | +# Read events from a Jms Broker. Supports both Jms Queues and Topics. |
| 7 | +# |
| 8 | +# For more information about Jms, see <http://docs.oracle.com/javaee/6/tutorial/doc/bncdq.html> |
| 9 | +# For more information about the Ruby Gem used, see <http://github.com/reidmorrison/jruby-jms> |
| 10 | +# Here is a config example : |
| 11 | +# jms { |
| 12 | +# include_header => false |
| 13 | +# include_properties => false |
| 14 | +# include_body => true |
| 15 | +# use_jms_timestamp => false |
| 16 | +# interval => 10 |
| 17 | +# queue_name => "myqueue" |
| 18 | +# yaml_file => "~/jms.yml" |
| 19 | +# yaml_section => "mybroker" |
| 20 | +# } |
| 21 | +# |
| 22 | +# |
| 23 | +class LogStash::Inputs::Jms < LogStash::Inputs::Threadable |
| 24 | + config_name "jms" |
| 25 | + milestone 1 |
| 26 | + |
| 27 | + default :config, "plain" |
| 28 | + |
| 29 | + # A JMS message has three parts : |
| 30 | + # Message Headers (required) |
| 31 | + # Message Properties (optional) |
| 32 | + # Message Bodies (optional) |
| 33 | + # You can tell the input plugin which parts should be included in the event produced by Logstash |
| 34 | + # |
| 35 | + # Include JMS Message Header Field values in the event |
| 36 | + config :include_header, :validate => :boolean, :default => true |
| 37 | + # Include JMS Message Properties Field values in the event |
| 38 | + config :include_properties, :validate => :boolean, :default => true |
| 39 | + # Include JMS Message Body in the event |
| 40 | + # Supports TextMessage and MapMessage |
| 41 | + # If the JMS Message is a TextMessage, then the value will be in the "message" field of the event |
| 42 | + # If the JMS Message is a MapMessage, then all the key/value pairs will be added in the Hashmap of the event |
| 43 | + # BytesMessage, StreamMessage and ObjectMessage are not supported |
| 44 | + config :include_body, :validate => :boolean, :default => true |
| 45 | + # Convert the JMSTimestamp header field to the @timestamp value of the event |
| 46 | + # Don't use it for now, it is buggy |
| 47 | + config :use_jms_timestamp, :validate => :boolean, :default => false |
| 48 | + |
| 49 | + # Choose an implementation of the run block. Value can be either consumer, async or thread |
| 50 | + config :runner, :validate => :string, :required => true, :default => "consumer" |
| 51 | + |
| 52 | + # Set the selector to use to get messages off the queue or topic |
| 53 | + config :selector, :validate => :string |
| 54 | + |
| 55 | + # Initial connection timeout in seconds. |
| 56 | + config :timeout, :validate => :number, :default => 1000 |
| 57 | + |
| 58 | + # Polling interval. |
| 59 | + # This is the time sleeping between asks to a consumed Queue. |
| 60 | + # This parameter has non influence in the case of a subcribed Topic. |
| 61 | + config :interval, :validate => :number, :default => 10 |
| 62 | + |
| 63 | + # If pub-sub (topic) style should be used or not. |
| 64 | + # Mandatory |
| 65 | + config :pub_sub, :validate => :boolean, :default => false |
| 66 | + # Name of the destination queue or topic to use. |
| 67 | + # Mandatory |
| 68 | + config :destination, :validate => :string |
| 69 | + |
| 70 | + # Yaml config file |
| 71 | + config :yaml_file, :validate => :string |
| 72 | + # Yaml config file section name |
| 73 | + # For some known examples, see: [Example jms.yml](https://github.com/reidmorrison/jruby-jms/blob/master/examples/jms.yml) |
| 74 | + config :yaml_section, :validate => :string |
| 75 | + |
| 76 | + # If you do not use an yaml configuration use either the factory or jndi_name. |
| 77 | + |
| 78 | + # An optional array of Jar file names to load for the specified |
| 79 | + # JMS provider. By using this option it is not necessary |
| 80 | + # to put all the JMS Provider specific jar files into the |
| 81 | + # java CLASSPATH prior to starting Logstash. |
| 82 | + config :required_jars, :validate => :array |
| 83 | + |
| 84 | + # Name of JMS Provider Factory class |
| 85 | + config :factory, :validate => :string |
| 86 | + # Username to connect to JMS provider with |
| 87 | + config :username, :validate => :string |
| 88 | + # Password to use when connecting to the JMS provider |
| 89 | + config :password, :validate => :string |
| 90 | + # Url to use when connecting to the JMS provider |
| 91 | + config :broker_url, :validate => :string |
| 92 | + |
| 93 | + # Name of JNDI entry at which the Factory can be found |
| 94 | + config :jndi_name, :validate => :string |
| 95 | + # Mandatory if jndi lookup is being used, |
| 96 | + # contains details on how to connect to JNDI server |
| 97 | + config :jndi_context, :validate => :hash |
| 98 | + |
| 99 | + # :yaml_file, :factory and :jndi_name are mutually exclusive, both cannot be supplied at the |
| 100 | + # same time. The priority order is :yaml_file, then :jndi_name, then :factory |
| 101 | + # |
| 102 | + # JMS Provider specific properties can be set if the JMS Factory itself |
| 103 | + # has setters for those properties. |
| 104 | + # |
| 105 | + # For some known examples, see: [Example jms.yml](https://github.com/reidmorrison/jruby-jms/blob/master/examples/jms.yml) |
| 106 | + |
| 107 | + public |
| 108 | + def register |
| 109 | + require "jms" |
| 110 | + @connection = nil |
| 111 | + |
| 112 | + if @yaml_file |
| 113 | + @jms_config = YAML.load_file(@yaml_file)[@yaml_section] |
| 114 | + |
| 115 | + elsif @jndi_name |
| 116 | + @jms_config = { |
| 117 | + :require_jars => @require_jars, |
| 118 | + :jndi_name => @jndi_name, |
| 119 | + :jndi_context => @jndi_context} |
| 120 | + |
| 121 | + elsif @factory |
| 122 | + @jms_config = { |
| 123 | + :require_jars => @require_jars, |
| 124 | + :factory => @factory, |
| 125 | + :username => @username, |
| 126 | + :password => @password, |
| 127 | + :broker_url => @broker_url, |
| 128 | + :url => @broker_url # "broker_url" is named "url" with Oracle AQ |
| 129 | + } |
| 130 | + end |
| 131 | + |
| 132 | + @logger.debug("JMS Config being used", :context => @jms_config) |
| 133 | + |
| 134 | + end # def register |
| 135 | + |
| 136 | + |
| 137 | + private |
| 138 | + def queue_event(msg, output_queue) |
| 139 | + begin |
| 140 | + event = LogStash::Event.new |
| 141 | + |
| 142 | + # Here, we can use the JMS Enqueue timestamp as the @timestamp |
| 143 | + if @use_jms_timestamp and msg.jms_timestamp |
| 144 | + event.timestamp = ::Time.at(msg.jms_timestamp/1000) |
| 145 | + end |
| 146 | + |
| 147 | + if @include_header |
| 148 | + #event.append(msg.attributes) |
| 149 | + msg.attributes.each do |field, value| |
| 150 | + event[field.to_s] = value |
| 151 | + end |
| 152 | + end |
| 153 | + |
| 154 | + if @include_properties |
| 155 | + #event.append(msg.properties) |
| 156 | + msg.properties.each do |field, value| |
| 157 | + event[field.to_s] = value |
| 158 | + end |
| 159 | + end |
| 160 | + |
| 161 | + if @include_body |
| 162 | + if msg.java_kind_of?(JMS::MapMessage) |
| 163 | + #event.append(msg.data) |
| 164 | + msg.data.each do |field, value| |
| 165 | + event[field.to_s] = value # TODO(claveau): needs codec.decode or converter.convert ? |
| 166 | + end |
| 167 | + |
| 168 | + elsif msg.java_kind_of?(JMS::TextMessage) |
| 169 | + @codec.decode(msg.to_s) do |event_message| |
| 170 | + # Copy out the header data into the message. |
| 171 | + event.to_hash.each do |k,v| |
| 172 | + event_message[k] = v |
| 173 | + end |
| 174 | + # Now lets overwrite the event. |
| 175 | + event = event_message |
| 176 | + end |
| 177 | + else |
| 178 | + @logger.error( "Unknown data type #{msg.data.class.to_s} in Message" ) |
| 179 | + end |
| 180 | + end |
| 181 | + |
| 182 | + decorate(event) |
| 183 | + output_queue << event |
| 184 | + |
| 185 | + rescue => e # parse or event creation error |
| 186 | + @logger.error("Failed to create event", :message => msg, :exception => e, |
| 187 | + :backtrace => e.backtrace); |
| 188 | + end |
| 189 | + end |
| 190 | + |
| 191 | + |
| 192 | + |
| 193 | + # Consume all available messages on the queue |
| 194 | + # sleeps some time, then consume again |
| 195 | + private |
| 196 | + def run_consumer(output_queue) |
| 197 | + JMS::Connection.session(@jms_config) do |session| |
| 198 | + while(true) |
| 199 | + if (@pub_sub) |
| 200 | + session.consume(:topic_name => @descriptor, :timeout=>@timeout, :selector => @selector) do |message| |
| 201 | + queue_event message, output_queue |
| 202 | + end |
| 203 | + else |
| 204 | + session.consume(:queue_name => @descriptor, :timeout=>@timeout, :selector => @selector) do |message| |
| 205 | + queue_event message, output_queue |
| 206 | + end |
| 207 | + end |
| 208 | + sleep @interval |
| 209 | + end |
| 210 | + end |
| 211 | + rescue LogStash::ShutdownSignal |
| 212 | + # Do nothing, let us quit. |
| 213 | + rescue => e |
| 214 | + @logger.warn("JMS Consumer died", :exception => e, :backtrace => e.backtrace) |
| 215 | + sleep(10) |
| 216 | + retry |
| 217 | + end # def run |
| 218 | + |
| 219 | + |
| 220 | + |
| 221 | + |
| 222 | + # Consume all available messages on the queue through a listener |
| 223 | + private |
| 224 | + def run_thread(output_queue) |
| 225 | + connection = JMS::Connection.new(@jms_config) |
| 226 | + connection.on_exception do |jms_exception| |
| 227 | + @logger.warn("JMS Exception has occurred: #{jms_exception}") |
| 228 | + end |
| 229 | + if (@pub_sub) |
| 230 | + connection.on_message(:topic_name => @destination, :selector => @selector) do |message| |
| 231 | + queue_event message, output_queue |
| 232 | + end |
| 233 | + else |
| 234 | + connection.on_message(:queue_name => @destination, :selector => @selector) do |message| |
| 235 | + queue_event message, output_queue |
| 236 | + end |
| 237 | + end |
| 238 | + connection.start |
| 239 | + while(true) |
| 240 | + @logger.debug("JMS Thread sleeping ...") |
| 241 | + sleep @interval |
| 242 | + end |
| 243 | + rescue LogStash::ShutdownSignal |
| 244 | + connection.close |
| 245 | + rescue => e |
| 246 | + @logger.warn("JMS Consumer died", :exception => e, :backtrace => e.backtrace) |
| 247 | + sleep(10) |
| 248 | + retry |
| 249 | + end # def run |
| 250 | + |
| 251 | + |
| 252 | + |
| 253 | + # Consume all available messages on the queue through a listener |
| 254 | + private |
| 255 | + def run_async(output_queue) |
| 256 | + JMS::Connection.start(@jms_config) do |connection| |
| 257 | + # Define exception listener |
| 258 | + # The problem here is that we do not handle any exception |
| 259 | + connection.on_exception do |jms_exception| |
| 260 | + @logger.warn("JMS Exception has occurred: #{jms_exception}") |
| 261 | + raise jms_exception |
| 262 | + end |
| 263 | + # Define Asynchronous code block to be called every time a message is received |
| 264 | + if (@pub_sub) |
| 265 | + connection.on_message(:topic_name => @destination, :selector => @selector) do |message| |
| 266 | + queue_event message, output_queue |
| 267 | + end |
| 268 | + else |
| 269 | + connection.on_message(:queue_name => @destination, :selector => @selector) do |message| |
| 270 | + queue_event message, output_queue |
| 271 | + end |
| 272 | + end |
| 273 | + # Since the on_message handler above is in a separate thread the thread needs |
| 274 | + # to do some other work. It will just sleep for 10 seconds. |
| 275 | + while(true) |
| 276 | + sleep @interval |
| 277 | + end |
| 278 | + end |
| 279 | + rescue LogStash::ShutdownSignal |
| 280 | + # Do nothing, let us quit. |
| 281 | + rescue => e |
| 282 | + @logger.warn("JMS Consumer died", :exception => e, :backtrace => e.backtrace) |
| 283 | + sleep(10) |
| 284 | + retry |
| 285 | + end # def run |
| 286 | + |
| 287 | + |
| 288 | + public |
| 289 | + def run(output_queue) |
| 290 | + case runner |
| 291 | + when "consumer" then |
| 292 | + run_consumer(output_queue) |
| 293 | + when "async" then |
| 294 | + run_async(output_queue) |
| 295 | + when "thread" then |
| 296 | + run_thread(output_queue) |
| 297 | + end |
| 298 | + end # def run |
| 299 | + |
| 300 | +end # class LogStash::Inputs::Jms |
0 commit comments