Skip to content

Commit

Permalink
Updated to Cloud Foundry v2.
Browse files Browse the repository at this point in the history
  • Loading branch information
scottfrederick committed Jun 24, 2013
1 parent 762df6d commit b6b7235
Show file tree
Hide file tree
Showing 24 changed files with 218 additions and 121 deletions.
18 changes: 18 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
target/

lib-cov
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz

pids
logs
results

npm-debug.log
node_modules

16 changes: 0 additions & 16 deletions README

This file was deleted.

11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
RabbitMQ Cloud Foundry Samples
==============================

This repository contains sample applications to demonstrate the use of [RabbitMQ](http://www.rabbitmq.com/) on [Cloud Foundry](http://www.cloudfoundry.com).

Examples are provided for the following languages/runtimes:

* [Java with Spring](spring)
* [Ruby with Rails](rails)
* [Ruby with Sinatra](sinatra)
* [Node.js](nodejs)
1 change: 1 addition & 0 deletions nodejs/Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: node app.js
7 changes: 0 additions & 7 deletions nodejs/README

This file was deleted.

9 changes: 9 additions & 0 deletions nodejs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
This is a simple Node.js application demonstrating the use of RabbitMQ on Cloud Foundry.

## Deploying to Cloud Foundry ##

After installing in the 'cf' [command-line interface](http://docs.cloudfoundry.com/docs/using/managing-apps/cf/) for Cloud Foundry, targeting a Cloud Foundry instance, and logging in, the application can be pushed using these commands:

$ cf push

The provided `manifest.yml` file will be used to provide the application parameters to Cloud Foundry. You may need to provide a different URL for the application if the `rabbitmq-node` URL is already being used in your Cloud Foundry domain. The `manifest.yml` file specifies a RabbitMQ services that is available on the [run.pivotal.io](http://docs.cloudfoundry.com/docs/dotcom/getting-started.html) Cloud Foundry services marketplace. You may need to change the details of the RabbitMQ service to push to a different Cloud Foundry instance.
15 changes: 11 additions & 4 deletions nodejs/app.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
require.paths.unshift('./node_modules');

var http = require('http');
var amqp = require('amqp');
var URL = require('url');
var htmlEscape = require('sanitizer/sanitizer').escape;

function rabbitUrl() {
if (process.env.VCAP_SERVICES) {
conf = JSON.parse(process.env.VCAP_SERVICES);
return conf['rabbitmq-2.4'][0].credentials.url;
var svcInfo = JSON.parse(process.env.VCAP_SERVICES);
for (var label in svcInfo) {
var svcs = svcInfo[label];
for (var index in svcs) {
var uri = svcs[index].credentials.uri;
if (uri.lastIndexOf("amqp", 0) == 0) {
return uri;
}
}
}
return null;
}
else {
return "amqp://localhost";
Expand Down
13 changes: 13 additions & 0 deletions nodejs/manifest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
applications:
- name: rabbitmq-node
memory: 256M
instances: 1
url: rabbitmq-node.${target-base}
path: .
services:
rabbit-sample:
label: cloudamqp
provider: cloudamqp
version: n/a
plan: lemur
3 changes: 1 addition & 2 deletions nodejs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{

"name":"node-srs-demo",
"name":"rabbitmq-node",
"author": "Michael Bridgen",
"version":"0.0.2",
"dependencies":{
Expand Down
6 changes: 0 additions & 6 deletions rails/README

This file was deleted.

9 changes: 9 additions & 0 deletions rails/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
This is a very simple Rails 3 application demonstrating the use of RabbitMQ on Cloud Foundry.

## Deploying to Cloud Foundry ##

After installing in the 'cf' [command-line interface](http://docs.cloudfoundry.com/docs/using/managing-apps/cf/) for Cloud Foundry, targeting a Cloud Foundry instance, and logging in, the application can be pushed using these commands:

$ cf push

The provided `manifest.yml` file will be used to provide the application parameters to Cloud Foundry. You may need to provide a different URL for the application if the `rabbitmq-rails` URL is already being used in your Cloud Foundry domain. The `manifest.yml` file specifies a RabbitMQ services that is available on the [run.pivotal.io](http://docs.cloudfoundry.com/docs/dotcom/getting-started.html) Cloud Foundry services marketplace. You may need to change the details of the RabbitMQ service to push to a different Cloud Foundry instance.
4 changes: 2 additions & 2 deletions rails/app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ def self.amqp_url
services = JSON.parse(ENV['VCAP_SERVICES'], :symbolize_names => true)
url = services.values.map do |srvs|
srvs.map do |srv|
if srv[:label] =~ /^rabbitmq-/
srv[:credentials][:url]
if srv[:credentials][:uri] =~ /^amqp/
srv[:credentials][:uri]
else
[]
end
Expand Down
5 changes: 4 additions & 1 deletion rails/config/application.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
require File.expand_path('../boot', __FILE__)

require 'rails/all'
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "rails/test_unit/railtie"

# If you have a Gemfile, require the gems listed there, including any gems
# you've limited to :test, :development, or :production.
Expand Down
13 changes: 13 additions & 0 deletions rails/manifest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
applications:
- name: rabbitmq-rails
memory: 256M
instances: 1
url: rabbitmq-rails.${target-base}
path: .
services:
rabbit-rails:
label: cloudamqp
provider: cloudamqp
version: n/a
plan: lemur
2 changes: 1 addition & 1 deletion sinatra/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ GEM
rack (1.3.2)
sinatra (1.2.6)
rack (~> 1.1)
tilt (< 2.0, >= 1.2.2)
tilt (>= 1.2.2, < 2.0)
thin (1.2.11)
daemons (>= 1.0.9)
eventmachine (>= 0.12.6)
Expand Down
2 changes: 0 additions & 2 deletions sinatra/README

This file was deleted.

9 changes: 9 additions & 0 deletions sinatra/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
This is a very simple Sinatra application demonstrating the use of RabbitMQ on Cloud Foundry.

## Deploying to Cloud Foundry ##

After installing in the 'cf' [command-line interface](http://docs.cloudfoundry.com/docs/using/managing-apps/cf/) for Cloud Foundry, targeting a Cloud Foundry instance, and logging in, the application can be pushed using these commands:

$ cf push

The provided `manifest.yml` file will be used to provide the application parameters to Cloud Foundry. You may need to provide a different URL for the application if the `rabbitmq-sinatra` URL is already being used in your Cloud Foundry domain. The `manifest.yml` file specifies a RabbitMQ services that is available on the [run.pivotal.io](http://docs.cloudfoundry.com/docs/dotcom/getting-started.html) Cloud Foundry services marketplace. You may need to change the details of the RabbitMQ service to push to a different Cloud Foundry instance.
3 changes: 3 additions & 0 deletions sinatra/config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require './rabbit'

run RabbitMQ.new
13 changes: 13 additions & 0 deletions sinatra/manifest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
applications:
- name: rabbitmq-sinatra
memory: 256M
instances: 1
url: rabbitmq-sinatra.${target-base}
path: .
services:
rabbit-sinatra:
label: cloudamqp
provider: cloudamqp
version: n/a
plan: lemur
145 changes: 74 additions & 71 deletions sinatra/rabbit.rb
Original file line number Diff line number Diff line change
@@ -1,91 +1,94 @@
require 'sinatra'
require 'sinatra/base'
require 'erb'
require 'cgi'

require 'bunny'
require 'json'

enable :sessions

# Extract the connection string for the rabbitmq service from the
# service information provided by Cloud Foundry in an environment
# variable.
def amqp_url
services = JSON.parse(ENV['VCAP_SERVICES'], :symbolize_names => true)
url = services.values.map do |srvs|
srvs.map do |srv|
if srv[:label] =~ /^rabbitmq-/
srv[:credentials][:url]
else
[]
class RabbitMQ < Sinatra::Base
enable :sessions

# Extract the connection string for the rabbitmq service from the
# service information provided by Cloud Foundry in an environment
# variable.
def amqp_url
services = JSON.parse(ENV['VCAP_SERVICES'], :symbolize_names => true)
url = services.values.map do |srvs|
srvs.map do |srv|
if srv[:credentials][:uri] =~ /^amqp/
srv[:credentials][:uri]
else
[]
end
end
end
end.flatten!.first
end
end.flatten!.first
end

# Opens a client connection to the RabbitMQ service, if one isn't
# already open. This is a class method because a new instance of
# the controller class will be created upon each request. But AMQP
# connections can be long-lived, so we would like to re-use the
# connection across many requests.
def client
unless $client
c = Bunny.new(amqp_url)
c.start
$client = c
# Opens a client connection to the RabbitMQ service, if one isn't
# already open. This is a class method because a new instance of
# the controller class will be created upon each request. But AMQP
# connections can be long-lived, so we would like to re-use the
# connection across many requests.
def client
unless $client
c = Bunny.new(amqp_url)
c.start
$client = c

# We only want to accept one un-acked message
$client.qos :prefetch_count => 1
# We only want to accept one un-acked message
$client.qos :prefetch_count => 1
end
$client
end
$client
end

# Return the "nameless exchange", pre-defined by AMQP as a means to
# send messages to specific queues. Again, we use a class method to
# share this across requests.
def nameless_exchange
$nameless_exchange ||= client.exchange('')
end
# Return the "nameless exchange", pre-defined by AMQP as a means to
# send messages to specific queues. Again, we use a class method to
# share this across requests.
def nameless_exchange
$nameless_exchange ||= client.exchange('')
end

# Return a queue named "messages". This will create the queue on
# the server, if it did not already exist. Again, we use a class
# method to share this across requests.
def messages_queue
$messages_queue ||= client.queue("messages")
end
# Return a queue named "messages". This will create the queue on
# the server, if it did not already exist. Again, we use a class
# method to share this across requests.
def messages_queue
$messages_queue ||= client.queue("messages")
end

def take_session key
res = session[key]
session[key] = nil
res
end
def take_session key
res = session[key]
session[key] = nil
res
end

get '/' do
@published = take_session(:published)
@got = take_session(:got)
erb :index
end
get '/' do
@published = take_session(:published)
@got = take_session(:got)
erb :index
end

post '/publish' do
# Send the message from the form's input box to the "messages"
# queue, via the nameless exchange. The name of the queue to
# publish to is specified in the routing key.
nameless_exchange.publish params[:message], :content_type => "text/plain",
:key => "messages"
# Notify the user that we published.
session[:published] = true
redirect to('/')
end
post '/publish' do
# Send the message from the form's input box to the "messages"
# queue, via the nameless exchange. The name of the queue to
# publish to is specified in the routing key.
nameless_exchange.publish params[:message], :content_type => "text/plain",
:key => "messages"
# Notify the user that we published.
session[:published] = true
redirect to('/')
end

post '/get' do
session[:got] = :queue_empty
post '/get' do
session[:got] = :queue_empty

# Wait for a message from the queue
messages_queue.subscribe(:ack => true, :timeout => 10,
:message_max => 1) do |msg|
# Show the user what we got
session[:got] = msg[:payload]
end
# Wait for a message from the queue
messages_queue.subscribe(:ack => true, :timeout => 10,
:message_max => 1) do |msg|
# Show the user what we got
session[:got] = msg[:payload]
end

redirect to('/')
redirect to('/')
end
end
6 changes: 0 additions & 6 deletions spring/README

This file was deleted.

10 changes: 10 additions & 0 deletions spring/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
This is a very simple Java and Spring application demonstrating the use of RabbitMQ on Cloud Foundry.

## Deploying to Cloud Foundry ##

After installing in the 'cf' [command-line interface](http://docs.cloudfoundry.com/docs/using/managing-apps/cf/) for Cloud Foundry, targeting a Cloud Foundry instance, and logging in, the application can be pushed using these commands:

$ mvn package
$ cf push

The provided `manifest.yml` file will be used to provide the application parameters to Cloud Foundry. You may need to provide a different URL for the application if the `rabbitmq-spring` URL is already being used in your Cloud Foundry domain. The `manifest.yml` file specifies a RabbitMQ services that is available on the [run.pivotal.io](http://docs.cloudfoundry.com/docs/dotcom/getting-started.html) Cloud Foundry services marketplace. You may need to change the details of the RabbitMQ service to push to a different Cloud Foundry instance.
Loading

0 comments on commit b6b7235

Please sign in to comment.