當前位置: 首頁>>代碼示例>>Golang>>正文


Golang NATSClient.Subscribe方法代碼示例

本文整理匯總了Golang中github.com/cloudfoundry/yagnats.NATSClient.Subscribe方法的典型用法代碼示例。如果您正苦於以下問題:Golang NATSClient.Subscribe方法的具體用法?Golang NATSClient.Subscribe怎麽用?Golang NATSClient.Subscribe使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/cloudfoundry/yagnats.NATSClient的用法示例。


在下文中一共展示了NATSClient.Subscribe方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: RemoteAuction

func RemoteAuction(client yagnats.NATSClient, auctionRequest types.AuctionRequest) types.AuctionResult {
	guid := util.RandomGuid()
	payload, _ := json.Marshal(auctionRequest)

	c := make(chan []byte)
	client.Subscribe(guid, func(msg *yagnats.Message) {
		c <- msg.Payload
	})

	client.PublishWithReplyTo("diego.auction", guid, payload)

	var responsePayload []byte
	select {
	case responsePayload = <-c:
	case <-time.After(time.Minute):
		return types.AuctionResult{}
	}

	var auctionResult types.AuctionResult
	err := json.Unmarshal(responsePayload, &auctionResult)
	if err != nil {
		panic(err)
	}

	return auctionResult
}
開發者ID:vito,項目名稱:auction,代碼行數:26,代碼來源:auctioneer.go

示例2: Register

func Register(c *VcapComponent, mbusClient yagnats.NATSClient) {
	mbusClient.Subscribe("vcap.component.discover", func(msg *yagnats.Message) {
		Component.Uptime = Component.Start.Elapsed()
		b, e := json.Marshal(Component)
		if e != nil {
			log.Warnf(e.Error())
			return
		}

		mbusClient.Publish(msg.ReplyTo, b)
	})

	b, e := json.Marshal(Component)
	if e != nil {
		log.Fatal(e.Error())
		panic("Component's information should be correct")
	}

	mbusClient.Publish("vcap.component.announce", b)

	log.Infof("Component %s registered successfully", Component.Type)
}
開發者ID:johntdyer,項目名稱:golang-devops-stuff,代碼行數:22,代碼來源:component.go

示例3: Register

func (c *VcapComponent) Register(mbusClient yagnats.NATSClient) error {
	mbusClient.Subscribe("vcap.component.discover", func(msg *yagnats.Message) {
		c.Uptime = c.StartTime.Elapsed()
		b, e := json.Marshal(c)
		if e != nil {
			log.Warnf(e.Error())
			return
		}

		mbusClient.Publish(msg.ReplyTo, b)
	})

	b, e := json.Marshal(c)
	if e != nil {
		log.Error(e.Error())
		return e
	}

	mbusClient.Publish("vcap.component.announce", b)

	log.Infof("Component %s registered successfully", c.Type)
	return nil
}
開發者ID:KeyOfSpectator,項目名稱:gorouter,代碼行數:23,代碼來源:component.go

示例4: NewStartStopListener

func NewStartStopListener(messageBus yagnats.NATSClient, conf *config.Config) *StartStopListener {
	listener := &StartStopListener{
		messageBus: messageBus,
	}

	messageBus.Subscribe(conf.SenderNatsStartSubject, func(message *yagnats.Message) {
		startMessage, err := models.NewStartMessageFromJSON([]byte(message.Payload))
		if err != nil {
			panic(err)
		}
		listener.Starts = append(listener.Starts, startMessage)
	})

	messageBus.Subscribe(conf.SenderNatsStopSubject, func(message *yagnats.Message) {
		stopMessage, err := models.NewStopMessageFromJSON([]byte(message.Payload))
		if err != nil {
			panic(err)
		}
		listener.Stops = append(listener.Stops, stopMessage)
	})

	return listener
}
開發者ID:KeyOfSpectator,項目名稱:hm9000,代碼行數:23,代碼來源:start_stop_listener.go

示例5: runSimulation

func runSimulation(natsClient yagnats.NATSClient) {
	simulationLock = &sync.Mutex{}
	simulationWait = &sync.WaitGroup{}
	taskTracker = map[string]*taskData{}

	msg := stagingMessage{
		Count:    nTasks,
		MemoryMB: taskMemory,
	}

	payload, err := json.Marshal(msg)
	if err != nil {
		panic(err)
	}

	t := time.Now()

	logger.Info("simulation.start", nTasks)

	simulationWait.Add(nTasks)

	_, err = natsClient.Subscribe("info.stager.*.staging-request.desire", func(msg *yagnats.Message) {
		var desiredLog struct {
			Timestamp time.Time   `json:"_timestamp"`
			Task      models.Task `json:"task"`
		}

		err := json.Unmarshal(msg.Payload, &desiredLog)
		if err != nil {
			panic(err)
		}

		registerDesired(desiredLog.Task.Guid, desiredLog.Timestamp)
	})

	_, err = natsClient.Subscribe("error.>", func(msg *yagnats.Message) {
		var errorLog struct {
			Timestamp time.Time `json:"_timestamp"`
			Error     string    `json:"error"`
		}

		err := json.Unmarshal(msg.Payload, &errorLog)
		if err != nil {
			panic(err)
		}

		registerError(msg.Subject+": "+errorLog.Error, errorLog.Timestamp)
	})

	_, err = natsClient.Subscribe("fatal.>", func(msg *yagnats.Message) {
		var errorLog struct {
			Timestamp time.Time `json:"_timestamp"`
			Error     string    `json:"error"`
		}

		err := json.Unmarshal(msg.Payload, &errorLog)
		if err != nil {
			panic(err)
		}

		registerError(msg.Subject+": "+errorLog.Error, errorLog.Timestamp)
	})

	executorIndexes := map[string]int{}

	_, err = natsClient.Subscribe("completed-task", func(msg *yagnats.Message) {
		defer func() {
			e := recover()
			if e != nil {
				logger.Error("RECOVERED PANIC:", e)
			}
		}()

		var task *models.Task

		err := json.Unmarshal(msg.Payload, &task)
		if err != nil {
			panic(err)
		}

		simulationLock.Lock()

		index, ok := executorIndexes[task.ExecutorID]
		if !ok {
			index = len(executorIndexes) + 1
			executorIndexes[task.ExecutorID] = index
		}

		data, ok := taskTracker[task.Guid]
		if !ok {
			logger.Error("uknown.runonce.completed", task.Guid, "executor", task.ExecutorID)
			simulationLock.Unlock()
			return
		}

		data.CompletionTime = float64(time.Now().UnixNano()) / 1e9
		logger.Info("runonce.completed", task.Guid, "executor", task.ExecutorID, "duration", data.CompletionTime-data.DesiredTime)
		data.ExecutorIndex = index
		data.NumCompletions++

//.........這裏部分代碼省略.........
開發者ID:vito,項目名稱:fake-diego-release,代碼行數:101,代碼來源:simulation.go


注:本文中的github.com/cloudfoundry/yagnats.NATSClient.Subscribe方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。