Skip to content

Commit 2018a5b

Browse files
committedJan 25, 2015
Initial code for JMS support.
Everything should be there that is needed. The rspec file doesn’t work yet though due to something strange with the jndi_context.
1 parent 1d27efe commit 2018a5b

File tree

7 files changed

+531
-1
lines changed

7 files changed

+531
-1
lines changed
 

‎.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*.gem
2+
Gemfile.lock
3+
.bundle
4+
vendor

‎Gemfile

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
source 'https://rubygems.org'
2+
gemspec
3+
gem "logstash", :github => "elasticsearch/logstash", :branch => "1.5"

‎README.md

+95-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,95 @@
1-
# logstash-input-jms
1+
# Logstash Plugin
2+
3+
This is a plugin for [Logstash](https://github.com/elasticsearch/logstash).
4+
5+
It is fully free and fully open source. The license is Apache 2.0, meaning you are pretty much free to use it however you want in whatever way.
6+
7+
## Documentation
8+
9+
Logstash provides infrastructure to automatically generate documentation for this plugin. We use the asciidoc format to write documentation so any comments in the source code will be first converted into asciidoc and then into html. All plugin documentation are placed under one [central location](http://www.elasticsearch.org/guide/en/logstash/current/).
10+
11+
- For formatting code or config example, you can use the asciidoc `[source,ruby]` directive
12+
- For more asciidoc formatting tips, see the excellent reference here https://github.com/elasticsearch/docs#asciidoc-guide
13+
14+
## Need Help?
15+
16+
Need help? Try #logstash on freenode IRC or the logstash-users@googlegroups.com mailing list.
17+
18+
## Developing
19+
20+
### 1. Plugin Developement and Testing
21+
22+
#### Code
23+
- To get started, you'll need JRuby with the Bundler gem installed.
24+
25+
- Create a new plugin or clone and existing from the GitHub [logstash-plugins](https://github.com/logstash-plugins) organization.
26+
27+
- Install dependencies
28+
```sh
29+
bundle install
30+
```
31+
32+
#### Test
33+
34+
```sh
35+
bundle exec rspec
36+
```
37+
38+
The Logstash code required to run the tests/specs is specified in the `Gemfile` by the line similar to:
39+
```ruby
40+
gem "logstash", :github => "elasticsearch/logstash", :branch => "1.5"
41+
```
42+
To test against another version or a local Logstash, edit the `Gemfile` to specify an alternative location, for example:
43+
```ruby
44+
gem "logstash", :github => "elasticsearch/logstash", :ref => "master"
45+
```
46+
```ruby
47+
gem "logstash", :path => "/your/local/logstash"
48+
```
49+
50+
Then update your dependencies and run your tests:
51+
52+
```sh
53+
bundle install
54+
bundle exec rspec
55+
```
56+
57+
### 2. Running your unpublished Plugin in Logstash
58+
59+
#### 2.1 Run in a local Logstash clone
60+
61+
- Edit Logstash `tools/Gemfile` and add the local plugin path, for example:
62+
```ruby
63+
gem "logstash-filter-awesome", :path => "/your/local/logstash-filter-awesome"
64+
```
65+
- Update Logstash dependencies
66+
```sh
67+
rake vendor:gems
68+
```
69+
- Run Logstash with your plugin
70+
```sh
71+
bin/logstash -e 'filter {awesome {}}'
72+
```
73+
At this point any modifications to the plugin code will be applied to this local Logstash setup. After modifying the plugin, simply rerun Logstash.
74+
75+
#### 2.2 Run in an installed Logstash
76+
77+
- Build your plugin gem
78+
```sh
79+
gem build logstash-filter-awesome.gemspec
80+
```
81+
- Install the plugin from the Logstash home
82+
```sh
83+
bin/plugin install /your/local/plugin/logstash-filter-awesome.gem
84+
```
85+
- Start Logstash and proceed to test the plugin
86+
87+
## Contributing
88+
89+
All contributions are welcome: ideas, patches, documentation, bug reports, complaints, and even something you drew up on a napkin.
90+
91+
Programming is not a required skill. Whatever you've seen about open source and maintainers or community members saying "send patches or die" - you will not see that here.
92+
93+
It is more important to me that you are able to contribute.
94+
95+
For more information about contributing, see the [CONTRIBUTING](https://github.com/elasticsearch/logstash/blob/master/CONTRIBUTING.md) file.

‎Rakefile

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@files=[]
2+
3+
task :default do
4+
system("rake -T")
5+
end
6+
7+
require "logstash/devutils/rake"

‎lib/logstash/inputs/jms.rb

+300
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,300 @@
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

‎logstash-input-jms.gemspec

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Gem::Specification.new do |s|
2+
3+
s.name = 'logstash-input-jms'
4+
s.version = '0.1.0'
5+
s.licenses = ['Apache License (2.0)']
6+
s.summary = "Pull events from a JMS topic or queue."
7+
s.description = "This gem is a logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not a stand-alone program"
8+
s.authors = ["Elasticsearch"]
9+
s.email = 'info@elasticsearch.com'
10+
s.homepage = "http://www.elasticsearch.org/guide/en/logstash/current/index.html"
11+
s.require_paths = ["lib"]
12+
13+
# Files
14+
s.files = `git ls-files`.split($\)+::Dir.glob('vendor/*')
15+
16+
# Tests
17+
s.test_files = s.files.grep(%r{^(test|spec|features)/})
18+
19+
# Special flag to let us know this is actually a logstash plugin
20+
s.metadata = { "logstash_plugin" => "true", "logstash_group" => "input" }
21+
22+
# Gem dependencies
23+
s.add_runtime_dependency 'logstash', '>= 1.4.0', '< 2.0.0'
24+
25+
s.add_runtime_dependency 'logstash-codec-json'
26+
27+
if RUBY_PLATFORM == 'java'
28+
s.platform = RUBY_PLATFORM
29+
s.add_runtime_dependency "jruby-jms" #(Apache 2.0 license)
30+
end
31+
s.add_development_dependency 'logstash-devutils'
32+
end

‎spec/inputs/jms_spec.rb

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
require "logstash/devutils/rspec/spec_helper"
2+
require "jms"
3+
4+
def populate(queue_name, content)
5+
require "logstash/event"
6+
7+
jms_config = {
8+
:jndi_name => "/ConnectionFactory",
9+
:jndi_context => {
10+
'java.naming.factory.initial'=> 'org.jnp.interfaces.NamingContextFactory',
11+
'java.naming.provider.url'=> 'jnp://localhost:1099',
12+
'java.naming.factory.url.pkgs'=> 'org.jboss.naming:org.jnp.interfaces',
13+
'java.naming.security.principal'=> 'guest',
14+
'java.naming.security.credentials'=> 'guest'},
15+
:require_jars => [
16+
"/Applications/hornetq-2.4.0.Final/lib/hornetq-commons.jar",
17+
"/Applications/hornetq-2.4.0.Final/lib/hornetq-core-client.jar",
18+
"/Applications/hornetq-2.4.0.Final/lib/hornetq-jms-client.jar",
19+
"/Applications/hornetq-2.4.0.Final/lib/jboss-jms-api.jar",
20+
"/Applications/hornetq-2.4.0.Final/lib/jnp-client.jar",
21+
"/Applications/hornetq-2.4.0.Final/lib/netty.jar"]
22+
}
23+
24+
JMS::Connection.session(jms_config) do |session|
25+
session.producer(:queue_name => queue_name) do |producer|
26+
producer.send(session.message(content))
27+
end
28+
end
29+
end
30+
31+
def process(pipeline, queue, content)
32+
sequence = 0
33+
Thread.new { pipeline.run }
34+
event = queue.pop
35+
pipeline.shutdown
36+
end # process
37+
38+
describe "inputs/jms", :jms => true do
39+
40+
41+
describe "read events from a queue" do
42+
queue_name = "ExampleQueue"
43+
content = "number " + (1000 + rand(50)).to_s
44+
config <<-CONFIG
45+
input {
46+
jms {
47+
jndi_name => "/ConnectionFactory"
48+
jndi_context => {
49+
"java.naming.factory.initial"=> "org.jnp.interfaces.NamingContextFactory"
50+
"java.naming.provider.url"=> "jnp://localhost:1099"
51+
"java.naming.factory.url.pkgs"=> "org.jboss.naming:org.jnp.interfaces"
52+
"java.naming.security.principal"=> "guest"
53+
"java.naming.security.credentials"=> "guest"
54+
}
55+
require_jars => [
56+
"/Applications/hornetq-2.4.0.Final/lib/hornetq-commons.jar",
57+
"/Applications/hornetq-2.4.0.Final/lib/hornetq-core-client.jar",
58+
"/Applications/hornetq-2.4.0.Final/lib/hornetq-jms-client.jar",
59+
"/Applications/hornetq-2.4.0.Final/lib/jboss-jms-api.jar",
60+
"/Applications/hornetq-2.4.0.Final/lib/jnp-client.jar",
61+
"/Applications/hornetq-2.4.0.Final/lib/netty.jar"
62+
]
63+
destination => "#{queue_name}"
64+
}
65+
}
66+
CONFIG
67+
68+
before(:each) { populate(queue_name, content) }
69+
70+
input { |pipeline, queue| process(pipeline, queue, content) }
71+
end
72+
73+
# describe "read events from a list with batch_count=5" do
74+
# key = 10.times.collect { rand(10).to_s }.join("")
75+
# event_count = 1000 + rand(50)
76+
# config <<-CONFIG
77+
# input {
78+
# redis {
79+
# type => "blah"
80+
# key => "#{key}"
81+
# data_type => "list"
82+
# batch_count => #{rand(20)+1}
83+
# }
84+
# }
85+
# CONFIG
86+
#
87+
# before(:each) { populate(key, event_count) }
88+
# input { |pipeline, queue| process(pipeline, queue, event_count) }
89+
# end
90+
end

0 commit comments

Comments
 (0)
Please sign in to comment.