"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const Event_1 = require("./model/Event");
const pulsar_client_1 = require("pulsar-client");
class EcPublisher {
    constructor() {
        this.pulsarProducerMap = new Map();
        //todo 定时拉取注册中心作缓存
        let pulsarClients = new Array();
        const client = new pulsar_client_1.Client({
            serviceUrl: 'pulsar://101.37.145.159:6650',
        });
        pulsarClients.push(client);
        this.pulsarClientArray = pulsarClients;
        // //todo 拉取注册中心看目前总公共有多少个目标环境
        // let pulsarProducers = new Array();
        // const producer = client.createProducer({
        //     topic: 'persistent://yongqiangTenant/yongqiangNamespace/' + Event.getTopic(),
        // }).then(() =>
        //     pulsarProducers.push(producer)
        // );
        // this.pulsarProducerArray = pulsarProducers;
    }
    publishMessage(payloadSchema, sendType) {
        // this.pulsarProducerArray.forEach(producer => {
        //     const msg = JSON.stringify(payloadSchema);
        //     console.log("开始发送pulsar数据:" + msg);
        //     producer.send({
        //         data: Buffer.from(msg),
        //     });
        //     console.log("发送pulsar数据成功！");
        // });
        this.pulsarClientArray.forEach(client => {
            (() => __awaiter(this, void 0, void 0, function* () {
                let topic = 'persistent://yongqiangTenant/yongqiangNamespace/' + Event_1.Event.getTopic();
                let producer = this.pulsarProducerMap.get(topic);
                if (producer == undefined) {
                    console.log("当前生产者为undefined...");
                    producer = yield client.createProducer({
                        topic: topic,
                    });
                    this.pulsarProducerMap.set(topic, producer);
                }
                else {
                    console.log("当前生产者正常...");
                }
                const msg = JSON.stringify(payloadSchema);
                console.log("开始发送pulsar数据:" + msg);
                producer.send({
                    data: Buffer.from(msg),
                });
                console.log("发送pulsar数据成功！");
            }))();
        });
    }
}
exports.EcPublisher = EcPublisher;
//# sourceMappingURL=EcPublisher.js.map