# subscribeAsync get tag changed

**URL:** <https://forum.inductiveautomation.com/t/subscribeasync-get-tag-changed/67749>\
**Category:** Module Development\
**Created:** [December 17, 2022, 12:35am UTC](https://forum.inductiveautomation.com/t/subscribeasync-get-tag-changed/67749 "2022-12-17T00:35:11Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ha99](https://avatars.discourse-cdn.com/v4/letter/h/7feea3/32.png) [@Ha99](https://forum.inductiveautomation.com/u/Ha99)\
**Post date:** [December 17, 2022, 12:35am UTC](https://forum.inductiveautomation.com/t/subscribeasync-get-tag-changed/67749/1 "2022-12-17T00:35:11Z")

</div>

I try to get changed value of a tag with subscribe Async :

I have this in setup(GatewayContext context) GatewayHook :  
GatewayTagManager tagManager = context.getTagManager();  
TagPath Tag1 = TagPathParser.parse("[Default]test1");  
TagListener tagChangeListener = new TagListener();  
tagManager.subscribeAsync(Tag1,tagChangeListener);

And in TagListener implements TagChangeListener :  
public void tagChanged(TagChangeEvent tagChangeEvent) throws InvalidListenerException {  
logger.info("value："+String.valueOf(tagChangeEvent.getValue()));  
logger.info("path"+String.valueOf(tagChangeEvent.getTagPath()));  
}  
But I don't see anything in the gateway when I change the value of the tag 😕

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 17, 2022, 1:35am UTC](https://forum.inductiveautomation.com/t/subscribeasync-get-tag-changed/67749/2 "2022-12-17T01:35:37Z")

</div>

You aren't doing anything with the return value of `.subscribeAsync()`, so you'll miss any exceptions that would have been reported to the CompletableFuture. You should use `.whenComplete()` to provide a handler that can log success or exception from the subscribe operation itself.

---

<div class="post-metadata">

**Author:** ![Ha99](https://avatars.discourse-cdn.com/v4/letter/h/7feea3/32.png) [@Ha99](https://forum.inductiveautomation.com/u/Ha99)\
**Post date:** [December 17, 2022, 11:49am UTC](https://forum.inductiveautomation.com/t/subscribeasync-get-tag-changed/67749/3 "2022-12-17T11:49:59Z")

</div>

Can i have an example how i can do this ?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 17, 2022, 2:31pm UTC](https://forum.inductiveautomation.com/t/subscribeasync-get-tag-changed/67749/4 "2022-12-17T14:31:32Z")

</div>

Assuming the logger is a `final` variable, it should look something like this:

```auto
tagManager.subscribeAsync(Tag1,tagChangeListener).whenComplete((v, t) -> {
	if (t == null)
		logger.info("Subscribe Success");
	else
		logger.warn("Subscribe Fail", t)
});

```

In the lambda above, `v` is a placeholder for the `CompletableFuture`'s result, but that is void for this interface, so not used. `t` is the placeholder for the `CompletableFuture`'s exception result, which you log if not null.

---

<div class="post-metadata">

**Author:** ![Ha99](https://avatars.discourse-cdn.com/v4/letter/h/7feea3/32.png) [@Ha99](https://forum.inductiveautomation.com/u/Ha99)\
**Post date:** [December 17, 2022, 3:40pm UTC](https://forum.inductiveautomation.com/t/subscribeasync-get-tag-changed/67749/5 "2022-12-17T15:40:05Z")

</div>

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/d/0/d001a17ca4059f79614a471d8c6d71250244c3a0.png)

I have a Success subscription, but when I change the value of the tags the tagChanged function does not start.

```auto
@Override
public void tagChanged(TagChangeEvent tagChangeEvent) throws InvalidListenerException {
     logger.info("value：" + String.valueOf(tagChangeEvent.getValue()));
     logger.info("path" + String.valueOf(tagChangeEvent.getTagPath()));
    }

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 17, 2022, 3:52pm UTC](https://forum.inductiveautomation.com/t/subscribeasync-get-tag-changed/67749/6 "2022-12-17T15:52:02Z")

</div>

I noticed that you are implementing a managed tag provider. I don't know enough about them to help much, but if the tag in question is **yours** (inside the managed provider), it matters **how** you are changing the value. **Something** must initiate the tag change notification. I suspect when implementing a managed provider, that would be **you**.

Try subscribing to a tag path that is in the default provider, and changing that tag.

---

<div class="post-metadata">

**Author:** ![Ha99](https://avatars.discourse-cdn.com/v4/letter/h/7feea3/32.png) [@Ha99](https://forum.inductiveautomation.com/u/Ha99)\
**Post date:** [December 17, 2022, 4:11pm UTC](https://forum.inductiveautomation.com/t/subscribeasync-get-tag-changed/67749/7 "2022-12-17T16:11:05Z")

</div>

```auto
 @Override
    public void setup(GatewayContext context) {
        try {
            this.context = context;
            GatewayTagManager tagManager = context.getTagManager();
            TagPath Tag1 = TagPathParser.parse("[test]testSDK1");
            TagListener tagChangeListener = new TagListener();
            //tagManager.subscribeAsync(Tag1,tagChangeListener).get(30, TimeUnit.SECONDS);.
            tagManager.subscribeAsync(Tag1,tagChangeListener).whenComplete((v, t) -> {
                if (t == null)
                    logger.info("Subscribe Success 1");
                else
                    logger.warn("Subscribe Fail 1", t);
            });

```

Here is what I do, I created a test provider and inside I have two tags testSDK1 and testSDK2 that I change manually from the designer.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 17, 2022, 4:13pm UTC](https://forum.inductiveautomation.com/t/subscribeasync-get-tag-changed/67749/8 "2022-12-17T16:13:49Z")

</div>

> [@Ha99](#):
>
> Here is what I do

I figured that out. I'm suggesting you try a variation that will isolate your problem to either the managed provider machinery or the listener machinery. Please add a listener to a tag that **isn't** in **your** provider and see if you get change events from it.

---

<div class="post-metadata">

**Author:** ![Ha99](https://avatars.discourse-cdn.com/v4/letter/h/7feea3/32.png) [@Ha99](https://forum.inductiveautomation.com/u/Ha99)\
**Post date:** [December 17, 2022, 4:28pm UTC](https://forum.inductiveautomation.com/t/subscribeasync-get-tag-changed/67749/9 "2022-12-17T16:28:37Z")

</div>

No it's the same behavior, I have no change, is there a specific config 😕

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 17, 2022, 5:35pm UTC](https://forum.inductiveautomation.com/t/subscribeasync-get-tag-changed/67749/10 "2022-12-17T17:35:58Z")

</div>

I don't have any module using the manage tag provider infrastructure, so I'm out of ideas.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 17, 2022, 5:40pm UTC](https://forum.inductiveautomation.com/t/subscribeasync-get-tag-changed/67749/11 "2022-12-17T17:40:04Z")

</div>

Actually, consider variable lifetimes. I don't know if the tag manager holds a direct reference to listeners or weak references to listeners. If the latter, your listener would no longer have a reference after your `.setup()` method exits, and would be garbage collected shortly thereafter. Try using a listener that has a lifetime anchored by a field in your hook class.

---

<div class="post-metadata">

**Author:** ![Ha99](https://avatars.discourse-cdn.com/v4/letter/h/7feea3/32.png) [@Ha99](https://forum.inductiveautomation.com/u/Ha99)\
**Post date:** [December 17, 2022, 6:17pm UTC](https://forum.inductiveautomation.com/t/subscribeasync-get-tag-changed/67749/12 "2022-12-17T18:17:11Z")

</div>

I tried but it doesn't work thank you for your help 🙂

---

<div class="post-metadata">

**Author:** ![Ha99](https://avatars.discourse-cdn.com/v4/letter/h/7feea3/32.png) [@Ha99](https://forum.inductiveautomation.com/u/Ha99)\
**Post date:** [December 18, 2022, 11:33am UTC](https://forum.inductiveautomation.com/t/subscribeasync-get-tag-changed/67749/13 "2022-12-18T11:33:10Z")

</div>

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/d/6d81e5b7ad4a429e84284c7ccdc644d0db531425.png)

now i have this error here is what i do

```auto
 @Override
    public void tagChanged(TagChangeEvent tagChangeEvent) {
        logger.info("test value=/%s"+tagChangeEvent.getValue().getValue());
        logger.info("test path=/%s"+tagChangeEvent.getTagPath());
    }

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 18, 2022, 1:57pm UTC](https://forum.inductiveautomation.com/t/subscribeasync-get-tag-changed/67749/14 "2022-12-18T13:57:06Z")

</div>

Examine the reported error line numbers. Add more logging until you narrow down which element is null when you aren't expecting a null. Then try to understand **why** it is null.
