Advertisement
vitareinforce

RMQ Flutter Boilerplate

Oct 29th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.60 KB | None | 0 0
  1. /**
  2.  * RMQ Class Boilerplate
  3.  * Created by Vitradisa Pratama
  4.  * E-Mail: vitradisa@pptik.itb.ac.id
  5.  */
  6.  
  7. /**
  8.  * Import dart_amqp library
  9.  */
  10. import 'package:dart_amqp/dart_amqp.dart';
  11.  
  12. /**
  13.  * Initialize Class
  14.  */
  15. class RMQ {
  16.  
  17.   /**
  18.    * Initialize RMQ Client, Channel, Consumer and String Message
  19.    */
  20.   Client client;
  21.   Channel channel;
  22.   Consumer consumer;
  23.   String strMessage;
  24.  
  25.   /**
  26.    * Initialize Parameter for RMQ Connection
  27.    */
  28.   static String _user  = "tmdgdai";
  29.   static String _pass  = "tmdgdai";
  30.   static String _vHost = "/tmdgdai";
  31.   static String _host  = "rmq1.pptik.id";
  32.  
  33.   /**
  34.    * Define Blank Constructor
  35.    */
  36.   RMQ();
  37.  
  38.   /**
  39.    * Encapsulate String Params
  40.    */
  41.   ConnectionSettings settings = new ConnectionSettings(
  42.     host:_host,
  43.     authProvider: new PlainAuthenticator(_user, _pass),
  44.     virtualHost:_vHost,
  45.   );
  46.  
  47.   /**
  48.    * Connect to RMQ
  49.    */
  50.   void connect() async {
  51.     client = new Client(settings: settings);
  52.     channel = await client.channel();
  53.   }
  54.  
  55.   /**
  56.    * Declare Exchange
  57.    * Params:
  58.    *  name : echange name
  59.    *  type : exchange type e.g: ExchangeType.Topic, ExchangeType.Fanout
  60.    *  _durable : durable settings: true or false
  61.    * Return as Exchange Object
  62.    */
  63.   Future<Exchange> declareExchange(name, type, _durable) {
  64.     return channel.exchange(name, type, durable: _durable);
  65.   }
  66.  
  67.   /**
  68.    * Declare Queue
  69.    * Params:
  70.    *  name : queue name
  71.    *  _durable : durable settings: true or false
  72.    * Return as Queue Object
  73.    */
  74.   Future<Queue> declareQueue(name, _durable) {
  75.     return channel.queue(name, durable: _durable);
  76.   }
  77.  
  78.   /**
  79.    * Bind Queue to Exchange and Routing Key
  80.    * Params:
  81.    *  exchange : exchange objects
  82.    *  queue : queue objects
  83.    *  routingKey : routingKey / Topic Name
  84.    */
  85.   void bind(exchange, queue, routingKey) {
  86.     queue.bind(exchange, routingKey);
  87.   }
  88.  
  89.   /**
  90.    * Publish Message to specific destinationObject
  91.    * Params:
  92.    *  destinationObject: object from declareQueue or declareExchange
  93.    */
  94.   void publish(destinationObject, message) async {
  95.     destinationObject.publish(message);
  96.   }
  97.  
  98.     /**
  99.    * Subscribe Message to specific destinationObject
  100.    * Params:
  101.    *  destination: object from declareQueue or declareExchange
  102.    */
  103.   void subscribe(destinationObject) async {
  104.     destinationObject.consume();
  105.     consumer.listen((AmqpMessage  message) async {
  106.       strMessage = message.payloadAsString;
  107.     });
  108.   }
  109.  
  110.   /**
  111.    * Get Subscribed Message
  112.    * Call subscribe function first
  113.    */
  114.   String getSubscribedData() {
  115.     return strMessage;
  116.   }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement