Giter Club home page Giter Club logo

amqp's Introduction

amqp

Use this library to make rpc calls over rabbit MQ using amqp protocol

Quickstart

Download library

go get -u github.com/raghuP9/amqp

Test with example

Generate amqpctl binary

go get -u github.com/raghuP9/amqp/cmd/amqpctl
$GOPATH/bin/amqpctl --help

Publish message

$GOPATH/bin/amqpctl --server rabbitmq.example.com --port 5672 --username <username> --password <password> producer

Consume message

$GOPATH/bin/amqpctl --server rabbitmq.example.com --port 5672 --username <username> --password <password> consumer

Use in go code

GoDoc Link

Create client object

package main

import (
  "github.com/raghuP9/amqp/blob/master/pkg/rpc/rmq"
)

func main() {
  client, err := rmq.GetRMQClient(
    true,                       // to connect securely i.e. using amqps or else set to false
    "rabbitmq-username",        // rabbitmq username
    "rabbitmq-password",        // rabbitmq password
    "myrabbitmq.server.com",    // rabbitmq server URL
    "1234",                     // rabbitmq server port
    "/"                         // vhost
  )
}

Declare exchange

err := client.ExchangeDeclare(
  "exchange-name",
  rmq.DefaultExchangeDeclareOpts(),
  rmq.DefaultConnectOpts(),
  )

Delete exchange

err := client.ExchangeDelete(
  "exchange-name",  // Exchange name
  true,             // IfUnused: Remove exchange if no queue bound to this exchange
  false,            // NoWait: Do not wait for deletion confirmation from rabbitmq server
)

Declare queue

err := client.QueueDeclare(
  "queue-name",
  rmq.DefaultDeclareQueueOpts(),
  rmq.DefaultConnectOpts(),
  )

Bind queue to an exchage using routing key

err := client.QueueBind(
  "exchange-name",
  "queue-name",
  "routing-key",
  rmq.DefaultQueueBindOpts(),
  rmq.DefaultConnectOpts(),
)

Delete queue

err := client.QueueDelete(
  "queue-name",
  rmq.DefaultQueueDeleteOpts(),
  rmq.DefaultConnectOpts(),
  )

Purge queue

err := client.QueuePurge(
  "queue-name",
  false,        // NoWait: do not wait for confirmation from rabbitmq server and return
  rmq.DefaultConnectOpts(),
)

Publish messages

import "github.com/streadway/amqp"

func doSomething() {
  err := client.Publish(
    amqp.Publishing{
      Body:         []byte(c.String("message")),
      DeliveryMode: amqp.Persistent,
      ContentType:  "plain/text",
      Timestamp:    time.Now(),
    },
    "exchange-name",
    "routing-key",
    rmq.DefaultPublishOpts(),
    rmq.DefaultConnectOpts(),
  )
}

Subscribe to a queue for messages and take actions on different messages

import "github.com/streadway/amqp"

func handler(msg amqp.Delivery) (amqp.Publishing, error) {
...
}

func doSomething() {
  err := client.Subscribe(
    context.TODO(),              // When passing context with cancel func, calling cancel() will return from Subscribe function
    "queue-name",                // Name of the queue
    rmq.DefaultSubscribeOpts(),  // Provide options such as correlation ID, listen indefinitely on the queue, reconnect if disconnected, publish response from handler function
    rmq.DefaultChannelOpts(),    // Set Qos e.g. do not pick another message from queue unless previous message is processed.
    rmq.DefaultConnectOpts(),
    handler,                     // handler function that takes received message, processes it and returns response message, can be anonymous fn.
  )
}

amqp's People

Contributors

raghurps avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.