Sunday, August 10, 2014

RabbitMQ Important Concepts

RabbitMQ some important concepts:

1. Channel: This channel is a virtual connection inside the “real” TCP connection, and it’s over the channel that you issue AMQP commands. Every channel has a unique ID assigned to it (your AMQP library of choice will handle remembering the ID for you). Whether you’re publishing a message, subscribing to a queue, or receiving a message, it’s all done over a channel.

2. Connection: a connection to a RabbitMQ server instance

3. Producers: Producers create messages and publish (send) them to a broker server (RabbitMQ).

4. Consumers: Consumers are just as simple. They attach to a broker server and subscribe to a queue. Think of a queue as a named mailbox.

5. What is the relationship between Channel and Queue? There is no direct relation between Channel and Queue. A Channel is used to send AMQP commands to the broker. This can be the creation of a queue or similar, but these concepts are not tied together.

6. What is the relationship between Queue and the Consumer Pool? Can multiple Consumers be subscribed to the same Queue? Can multiple Queues be consumed by the same Consumer? Or is the relationship 1:1?
It is also possible to attach the same Consumer to multiple Queues. You can understand Consumers as callbacks. These are called everytime a message arrives on a Queue the Consumer is bound to. For the case of the Java Client, each Consumers has a method handleDelivery(...), which represents the callback method. What you typically do is, subclass DefaultConsumer and override handleDelivery(...). Note: If you attach the same Consumer instance to multiple queues, this method will be called by different threads. So take care of synchronization if necessary.

Processing Learning 3

float d = dist(100, 100, mouseX, mouseY);  // Calculate the distance between (100, 100) and (mouseX, mouseY)

dist():
float distance(float x1, float y1, float x2. float y2) {
float dx = x1 - x2;
float dy = y1 - y2;
float d = sqrt(dx * dx + dy * dy);
return d;
}

Append():
Processing, in fact, offers a set of array functions that manipulate the sie of an array by managing this process for you. They are: shorten(), concat(), subset(), append(), and expand(). In addition, there are functions for changing the order in an array, such as sort() and reverse().

append(): Each time the mouse is pressed, a new object is created and appended to the end of the original array.

Built-in Library:

  • Video: For capturing images from a camera, playing movie files, and recording movie files.

  • Serial: For sending data between Processing and an external device via serial communication.

  • OpenGL: For rendering a sketch with graphics acceleration.

  • Network: For creating client and server sketches that can communicate across the internet.

  • PDF: For creating high resolution PDF's graphics generated in Processing.

  • XML: For importing data from XML documents.


float r = 75;
float theta = PI / 4; // We could also say: float theta = radian(45);
float x = r * cos(theta);
float y = r * sin(theta);

Processing Learning 2

System Variables:

  • width: Width (in pixels) of sketch window

  • height: Height (in pixels) of sketch window

  • framwCount: Number of frames processed

  • frameRate: Rate that frames are processed (per second)

  • screen.width: Width (in pixels) of entire screen

  • screen.height: Height (in pixels) of entire screen

  • key: Most recent key pressed on the keyboard

  • keyCode: Numeric code for key pressed on keyboard

  • KeyPressed: True or False? Is a Key pressed?

  • mousePressed: True or False? Is the mouse pressed?

  • mouseButton: Which button is pressed? Left, right, or center?


random():
float x = random(1, 100);
rect(100, 100, w, 50); //A random float number between 1 and 100

int w = int(random(1, 100));
rect(100, 100, w, 50); //A random integer number between 1 and 100

constrain():
r = constrain(r, 0, 255);
g = constrain(g, 0, 255);
b = constrain(b, 0, 255); //Constrain all color values to between 0 and 255

Thursday, August 7, 2014

Processing Learning 1

Pixels

  • - Specifying pixel coordinates

  • - Basic shapes: point, line, rectangle, eliipse

  • - Color: grayscale, "RGB"

  • - Color transparency


alpha: 0 being completely transparent (0% opaque) ans 255 completely opaque (100% opaque).

Processing

  • - pmouseX: previous mouseX location

  • - pmouseY: previous mouseY location


mousePressed(): Handles mouse clicks
keyPressed(): Handles key presses
frameRate(): which requires an integer between 1 and 60, enforces the speed at which Processing will cycle through draw(). frameRate(30) means 30 frames per second, a traditional speed for computer animation. If you do not include frameRate(), Processing will attempt to run sketch at 60 frames per second. Since computers run at a different speeds, frameRtae() is used to make sure that your sketch is consistent across multiple computers.

Color for given shape needs to be stored in the computer's memory. This memory is just a long sequence of 0's and 1's (a whole bunch of on or off switches). Each one of these switches is a bit, eight of them together is a byte. Imagine if we had eight bits in sequence - how many ways can we configure these switches? The answer is 256 possibilities, or a range of numbers between 0 and 255. We will use eight bit color for your grayscale range and 24 bit for full color (eight bits for each of the red, green, and blue color components).

How do you handle recovering from a faulty connection using RabbitMQjava client library?

I see this topic on the StackOverFlow, it's really useful so I decided to make it organized and repost. Hope this will be useful:
Hi,

I'm interested in knowing how other people handle recovering from a faulty connection using the official RabbitMQ java client library. We are using it to connect our application servers to our RabbitMQ cluster and we have implemented a few different ways to recover from a connection failure, but non of them feel quite right.
Imagine this pseudo application:

public class OurClassThatStartsConsumers {
  Connection conn;

  public void start() {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setUsername("someusername");
    factory.setPassword("somepassword");
    factory.setHost("somehost");
    conn = factory.newConnection();

    new Thread(new Consumer(conn.createChannel())).start();
  }
}
class Consumer1 implements Runnable {
  public Consumer1(Channel channel) {
  this.channel = channel;
  }

  @Override
  public void run() {
    while (true) {
    ... consume incoming messages on the channel...
    // How do we handle that the connection dies?
    }
  }
}

In the real world we have several hundreds of consumers. So what happens if
the connection dies? In the above example Consumer1 can not recover, when
the connection closes, the Channel also closes, a state from which we can
not recover. So lets look at some ways to solve this:

Solution A)
Let every consumer have their own connection and register the events that triggers when the connection dies and then handle reconnecting.

Pros: It works

Cons:

- Since we have a lot of consumers, we probably do not want that many connections.
- We might possible have a lot of duplicated code for reconnecting to rabbit and handle reconnecting

Solution B)
Have each consumer use the same connection and subscribe to its connection failure events.

Pros: Less connections than in Solution A

Cons: Since the connection is closed we need to reopen/replace it. The java client library doesn't seem to provide a way to reopen the connection, so we would have to replace it with a new connection and then somehow notify all the consumers about this new connection and they would have to recreate the channels and the consumers. Once again, a lot of logic that I don't want to see in the consumer ends up there.

Solution C)
Wrap Connection and Channel classes is classes that handle the re-connection logic, the consumer only needs to know about the WrappedChannel class. On a connection failure the WrappedConnection with deal with re-establishing the connection and once connected the
WrappedConnection will automatically create new Channels and register consumers.

Pros: It work - this is actually the solution we are using today.

Cons: It feels like a hack, I think this is something that should be handled more elegantly by the underlying library.

Maybe there is a much better way? The API documentation does not talk that
much about recovering from a faulty connection. Any input is appreciated :)

Comment 1:
This is what the two clients built on top of the Java one — Langohr and March Hare — do. It’s not a hack but a necessary work around because connection recovery is currently not performed by the Java client (it should be a core feature, if you ask me).

So this is a viable approach.

Take a look at Lyra, too: https://github.com/jhalterman/lyra.

Comment 2:
Solution C is actually pretty reasonable. There's not much to gain from using multiple connections to the same server if you're trying to guard against network failures or cluster partitions. If one connection dies, they all likely will. Wrapping and recovering connections/channels works fine, and as Michael mentioned, you might also check out Lyra since it handles the various corner cases involved in recovering resources for you.

Wednesday, August 6, 2014

RabbitMQ: com.rabbitmq.client.ShutdownSignalException

Here is the Exception when I encountered learning RabbitMQ:
com.rabbitmq.client.ShutdownSignalException: channel error; reason:
{#method<channel.close>(reply-code=406, reply-text=PRECONDITION_FAILED -
unknown delivery tag 1, class-id=60, method-id=80), null, ""}

The error you could arise if an application acknowledges messages incorrectly.
So you need to primary check these declarations: to check the "autoAck" attribute , make sure that messages are only acknowledged once.

Server side:

channel.basicConsume(QUEUE_NAME, false, CONSUMER_TAG_QUEUE, consumer);

Client Side:
channel.basicConsume(replyQueueName, false, consumer);

There are legitimate ways in which multiple consumers could get hold of the same message, in the same second. If any consumer rejects a message or reconnects then unacknowledged messages will be returned to the broker and may be delivered to other consumers. Such messages will have the redelivered flag set.

Tuesday, August 5, 2014

How to install Pika for Python on Windows

I am learning RabbitMQ these days and I follow the tutorial on http://www.rabbitmq.com/tutorials/tutorial-one-python.html,

And in this tutorial series they're going to use pika. To install it, it says:

  • On Windows: To install easy_install, run the MS Windows Installer for setuptools
easy_install pip
pip install pika==0.9.8
I don't know what is "easy_install" means, and finally I found out:
The "easy_install" is under the  python installation directory(Python/Scripts), there is an easy install.exe under Scripts directory, at the command line input:
easy_install.exe pip
pip.exe install pika