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


Golang NATSConn.Subscribe方法代碼示例

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


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

示例1: NewStartStopListener

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

	messageBus.Subscribe(conf.SenderNatsStartSubject, func(message *nats.Msg) {
		startMessage, err := models.NewStartMessageFromJSON([]byte(message.Data))
		if err != nil {
			panic(err)
		}
		listener.mutex.Lock()
		listener.starts = append(listener.starts, startMessage)
		listener.mutex.Unlock()
	})

	messageBus.Subscribe(conf.SenderNatsStopSubject, func(message *nats.Msg) {
		stopMessage, err := models.NewStopMessageFromJSON([]byte(message.Data))
		if err != nil {
			panic(err)
		}
		listener.mutex.Lock()
		listener.stops = append(listener.stops, stopMessage)
		listener.mutex.Unlock()
	})

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

示例2: Register

func (c *VcapComponent) Register(mbusClient yagnats.NATSConn) error {
	mbusClient.Subscribe("vcap.component.discover", func(msg *nats.Msg) {
		if msg.Reply == "" {
			log.Warnf("Received message with empty reply on subject %s", msg.Subject)
			return
		}

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

		mbusClient.Publish(msg.Reply, 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:jungle0755,項目名稱:gorouter,代碼行數:28,代碼來源:component.go

示例3: StartFakeRouter

func StartFakeRouter() {

	var startMessage = func() []byte {
		d := RouterStart{
			MinimumRegisterIntervalInSeconds: 20,
		}

		value, _ := json.Marshal(d)
		return value
	}

	natsMembers := make([]string, 1)
	uri := url.URL{
		Scheme: "nats",
		User:   url.UserPassword("", ""),
		Host:   "127.0.0.1:4222",
	}
	natsMembers = append(natsMembers, uri.String())

	var natsClient yagnats.NATSConn
	var err error
	for i := 0; i < 10; i++ {
		natsClient, err = yagnats.Connect(natsMembers)
		if err == nil {
			break
		}
		time.Sleep(time.Second)
	}
	Expect(err).ToNot(HaveOccurred())

	natsClient.Subscribe("router.register", func(msg *nats.Msg) {
	})
	natsClient.Subscribe("router.greet", func(msg *nats.Msg) {
		natsClient.Publish(msg.Reply, startMessage())
	})

	natsClient.Publish("router.start", startMessage())
}
開發者ID:lyuyun,項目名稱:loggregator,代碼行數:38,代碼來源:integration_test_suite_test.go

示例4:

		if natsRunner != nil {
			natsRunner.Stop()
		}

		if router != nil {
			router.Stop()
		}
	})

	Context("NATS", func() {
		Context("Router Greetings", func() {
			It("RouterGreets", func() {
				response := make(chan []byte)

				mbusClient.Subscribe("router.greet.test.response", func(msg *nats.Msg) {
					response <- msg.Data
				})

				mbusClient.PublishRequest("router.greet", "router.greet.test.response", []byte{})

				var msg []byte
				Eventually(response).Should(Receive(&msg))

				var message vcap.RouterStart
				err := json.Unmarshal(msg, &message)

				Expect(err).NotTo(HaveOccurred())
				Expect(message.MinimumRegisterIntervalInSeconds).To(Equal(5))
				Expect(message.PruneThresholdInSeconds).To(Equal(120))
			})
開發者ID:shashankmjain,項目名稱:gorouter,代碼行數:30,代碼來源:router_test.go

示例5:

			component.Varz.Type = "TestType"
			component.Logger = logger

			err := component.Start()
			Expect(err).ToNot(HaveOccurred())

			err = component.Register(mbusClient)
			Expect(err).ToNot(HaveOccurred())

			_, err = mbusClient.Subscribe("subject", func(msg *nats.Msg) {
				defer GinkgoRecover()
				data := make(map[string]interface{})
				err := json.Unmarshal(msg.Data, &data)
				Expect(err).ToNot(HaveOccurred())

				for _, key := range members {
					_, ok := data[key]
					Expect(ok).To(BeTrue())
				}

				close(done)
			})
			Expect(err).ToNot(HaveOccurred())

			err = mbusClient.PublishRequest("vcap.component.discover", "subject", []byte(""))
			Expect(err).ToNot(HaveOccurred())

			Eventually(done).Should(BeClosed())
		})

		It("publishes to vcap.component.announce on start-up", func() {
開發者ID:idouba,項目名稱:gorouter,代碼行數:31,代碼來源:component_test.go


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