Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * RMQ Class Boilerplate
- * Created by Vitradisa Pratama
- * E-Mail: vitradisa@pptik.itb.ac.id
- */
- /**
- * Import dart_amqp library
- */
- import 'package:dart_amqp/dart_amqp.dart';
- /**
- * Initialize Class
- */
- class RMQ {
- /**
- * Initialize RMQ Client, Channel, Consumer and String Message
- */
- Client client;
- Channel channel;
- Consumer consumer;
- String strMessage;
- /**
- * Initialize Parameter for RMQ Connection
- */
- static String _user = "tmdgdai";
- static String _pass = "tmdgdai";
- static String _vHost = "/tmdgdai";
- static String _host = "rmq1.pptik.id";
- /**
- * Define Blank Constructor
- */
- RMQ();
- /**
- * Encapsulate String Params
- */
- ConnectionSettings settings = new ConnectionSettings(
- host:_host,
- authProvider: new PlainAuthenticator(_user, _pass),
- virtualHost:_vHost,
- );
- /**
- * Connect to RMQ
- */
- void connect() async {
- client = new Client(settings: settings);
- channel = await client.channel();
- }
- /**
- * Declare Exchange
- * Params:
- * name : echange name
- * type : exchange type e.g: ExchangeType.Topic, ExchangeType.Fanout
- * _durable : durable settings: true or false
- * Return as Exchange Object
- */
- Future<Exchange> declareExchange(name, type, _durable) {
- return channel.exchange(name, type, durable: _durable);
- }
- /**
- * Declare Queue
- * Params:
- * name : queue name
- * _durable : durable settings: true or false
- * Return as Queue Object
- */
- Future<Queue> declareQueue(name, _durable) {
- return channel.queue(name, durable: _durable);
- }
- /**
- * Bind Queue to Exchange and Routing Key
- * Params:
- * exchange : exchange objects
- * queue : queue objects
- * routingKey : routingKey / Topic Name
- */
- void bind(exchange, queue, routingKey) {
- queue.bind(exchange, routingKey);
- }
- /**
- * Publish Message to specific destinationObject
- * Params:
- * destinationObject: object from declareQueue or declareExchange
- */
- void publish(destinationObject, message) async {
- destinationObject.publish(message);
- }
- /**
- * Subscribe Message to specific destinationObject
- * Params:
- * destination: object from declareQueue or declareExchange
- */
- void subscribe(destinationObject) async {
- destinationObject.consume();
- consumer.listen((AmqpMessage message) async {
- strMessage = message.payloadAsString;
- });
- }
- /**
- * Get Subscribed Message
- * Call subscribe function first
- */
- String getSubscribedData() {
- return strMessage;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement