# Messages sent by Spring JmsTemplate not being received by normal JCSMP subscriber

**URL:** <https://community.solace.com/t/messages-sent-by-spring-jmstemplate-not-being-received-by-normal-jcsmp-subscriber/115>\
**Category:** Solace Event Broker\
**Created:** [November 22, 2019, 6:25pm UTC](https://community.solace.com/t/messages-sent-by-spring-jmstemplate-not-being-received-by-normal-jcsmp-subscriber/115 "2019-11-22T18:25:00Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![alinaqvi](https://avatars.discourse-cdn.com/v4/letter/a/fbc32d/32.png) [@alinaqvi](https://community.solace.com/u/alinaqvi)\
**Post date:** [November 22, 2019, 6:25pm UTC](https://community.solace.com/t/messages-sent-by-spring-jmstemplate-not-being-received-by-normal-jcsmp-subscriber/115/1 "2019-11-22T18:25:00Z")

</div>

I am able to successfully send and receive topic messages via Spring JMS but the messages are not being received by a normal [Topic subscriber](https://github.com/SolaceSamples/solace-samples-jms/blob/master/src/main/java/com/solace/samples/TopicSubscriber.java "Topic subscriber").

My jms code for sending and receiving is below:

```
  @Autowired
    private JmsTemplate jmsTemplate;

    public void run(String... strings) throws Exception {

       
        for( int i = 0 ; i < 100; i++) {
        String msg = "Hello World" + i;
        jmsTemplate.setPubSubDomain(true);
        //jmsTemplate.setdu
        logger.info("============= Sending " + msg);
       this.jmsTemplate.convertAndSend("abc/topic2", msg);
       
        Thread.sleep(1000);
        }
    }
}

@Component
static class MessageHandler {

    private static final Logger logger = LoggerFactory.getLogger(MessageHandler.class);

    
    @JmsListener(destination = "abc/topic2", containerFactory = "cFactory", concurrency = "2")
    public void processMsg(Message<?> msg) {
    // Works fine, no issues. 
    	StringBuffer msgAsStr = new StringBuffer("============= Received \nHeaders:");
    	MessageHeaders hdrs = msg.getHeaders();
    	msgAsStr.append("\nUUID: "+hdrs.getId());
    	msgAsStr.append("\nTimestamp: "+hdrs.getTimestamp());
    	Iterator<String> keyIter = hdrs.keySet().iterator();
    	while (keyIter.hasNext()) {
    		String key = keyIter.next();
        	msgAsStr.append("\n"+key+": "+hdrs.get(key));
    	}
    	msgAsStr.append("\nPayload: "+msg.getPayload());
        logger.info(msgAsStr.toString());
    }

 public CachingConnectionFactory producerCachingConnectionFactory() throws Exception {
    CachingConnectionFactory ccf = new CachingConnectionFactory(connectionFactory());
    ccf.setSessionCacheSize(1);
    
    return ccf;
}

@Bean
public JmsTemplate jmsTemplate() throws Exception {

    // CachingConnectionFactory ccf = new CachingConnectionFactory(cf);
    JmsTemplate jsmTemplate = new JmsTemplate(producerCachingConnectionFactory() );
    jsmTemplate.setPubSubDomain(true);
    jsmTemplate.setDeliveryPersistent(false); // tried both true and false. 
            
    //jsmTemplate.setSessionTransacted(true); // I get an exception ... isnt supported with Direct transport.
    return jsmTemplate;
}

```

@Bean  
public ConnectionFactory connectionFactory() throws Exception {

```
    Hashtable<String, Object> env = new Hashtable<>();

    env.put(InitialContext.PROVIDER_URL, jndiProviderURL);
    env.put(Context.SECURITY_PRINCIPAL, username);
    env.put(Context.SECURITY_CREDENTIALS, password);
       System.setProperty("java.security.krb5.conf", KRB5_LOC);
    System.setProperty("java.security.auth.login.config", JAAS_LOC);
    env.put(SupportedProperty.SOLACE_JMS_AUTHENTICATION_SCHEME, 
   SupportedProperty.AUTHENTICATION_SCHEME_GSS_KRB);
    env.put(SupportedProperty.SOLACE_JMS_KRB_SERVICE_NAME, "HOST");
    env.put(SupportedProperty.SOLACE_JMS_VPN, vpn);
    env.put(SupportedProperty.SOLACE_JMS_DYNAMIC_DURABLES, false);
            env.put(SupportedProperty.SOLACE_JMS_DELIVER_TO_ONE_OVERRIDE , false);
          SolConnectionFactory cf = SolJmsUtility.createConnectionFactory(env );
    return cf;

}

@Bean
public DefaultJmsListenerContainerFactory cFactory(ConnectionFactory connectionFactory, DemoErrorHandler errorHandler) {
    DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
    factory.setConnectionFactory(connectionFactory);
    factory.setErrorHandler(errorHandler);
    factory.setPubSubDomain(true);
    factory.setSubscriptionDurable(false);
    return factory;
}

```

I wonder if you can spot some obvious issue here please?

Many Thanks,  
Ali

---

<div class="post-metadata">

**Author:** ![marc](https://yyz1.discourse-cdn.com/solace/user_avatar/community.solace.com/marc/32/2191_2.png) [@marc](https://community.solace.com/u/marc)\
**Post date:** [November 22, 2019, 7:30pm UTC](https://community.solace.com/t/messages-sent-by-spring-jmstemplate-not-being-received-by-normal-jcsmp-subscriber/115/2 "2019-11-22T19:30:28Z")

</div>

Hi Ali,  
To clarify a few things:

- The MessageHandler in the code you’re sharing is indeed receiving and processing the message?
- Your other receiver, not in the post, is just the TopicSubscriber.java class as is with only a change to the `TOPIC_NAME` variable?
- Can you share the rest of your class above where you autowire the jmstemplate?

thanks,  
Marc

---

<div class="post-metadata">

**Author:** ![alinaqvi](https://avatars.discourse-cdn.com/v4/letter/a/fbc32d/32.png) [@alinaqvi](https://community.solace.com/u/alinaqvi)\
**Post date:** [November 25, 2019, 11:14am UTC](https://community.solace.com/t/messages-sent-by-spring-jmstemplate-not-being-received-by-normal-jcsmp-subscriber/115/3 "2019-11-25T11:14:26Z")

</div>

Some thing wrong with the edit functionality of this forum … posts get deleted magically.

---

<div class="post-metadata">

**Author:** ![alinaqvi](https://avatars.discourse-cdn.com/v4/letter/a/fbc32d/32.png) [@alinaqvi](https://community.solace.com/u/alinaqvi)\
**Post date:** [November 25, 2019, 11:18am UTC](https://community.solace.com/t/messages-sent-by-spring-jmstemplate-not-being-received-by-normal-jcsmp-subscriber/115/4 "2019-11-25T11:18:07Z")

</div>

Attempt No2 for a reply… pls find my answers inline.

> [@marc](#):
>
> Hi Ali,  
> To clarify a few things:
> 
> - The MessageHandler in the code you’re sharing is indeed receiving and processing the message?

Yes that is correct.

> - Your other receiver, not in the post, is just the TopicSubscriber.java class as is with only a change to the `TOPIC_NAME` variable?  
> Yap, code is below which is tried and tested as its part of a gui tool we use for testing on daily basis.

```
private void subscribeSolaceTopic(String pTopic) {

    try {
        solaceConsumer = solaceSession.getMessageConsumer(new XMLMessageListener() {

            public void onReceive(BytesXMLMessage msg) {
                LOG.info("onReceive->Message Dump: {}", msg.dump());
                
                });
        
                
            }

            public void onException(JCSMPException e) {
                LOG.error("Consumer received exception: {}", e);
            }
        });

        Topic topic = JCSMPFactory.onlyInstance().createTopic(pTopic);

        solaceTopic = topic;

        solaceSession.addSubscription(topic);

        LOG.info("Connected. Awaiting message...");
        solaceConsumer.start();

    } catch (JCSMPException e1) {
        LOG.error(EXCEPTION_OCCURRED , e1);
        
    }

}

```

> - Can you share the rest of your class above where you autowire the jmstemplate?

The autowire is done on a field and its up there near the top of my first post. If you want I can try attaching the entire project ?

Thanks,  
Ali

---

<div class="post-metadata">

**Author:** ![alinaqvi](https://avatars.discourse-cdn.com/v4/letter/a/fbc32d/32.png) [@alinaqvi](https://community.solace.com/u/alinaqvi)\
**Post date:** [November 25, 2019, 11:20am UTC](https://community.solace.com/t/messages-sent-by-spring-jmstemplate-not-being-received-by-normal-jcsmp-subscriber/115/5 "2019-11-25T11:20:37Z")

</div>

Could it be that the call below is adding some thing to the name of the topic ?  
this.jmsTemplate.convertAndSend(“abc/topic2”, msg)

---

<div class="post-metadata">

**Author:** ![alinaqvi](https://avatars.discourse-cdn.com/v4/letter/a/fbc32d/32.png) [@alinaqvi](https://community.solace.com/u/alinaqvi)\
**Post date:** [November 25, 2019, 1:03pm UTC](https://community.solace.com/t/messages-sent-by-spring-jmstemplate-not-being-received-by-normal-jcsmp-subscriber/115/6 "2019-11-25T13:03:30Z")

</div>

Please ignore this for now … I might have found the root cause for this. Will come back if the issue still persists.  
Thanks,  
Ali

---

<div class="post-metadata">

**Author:** ![marc](https://yyz1.discourse-cdn.com/solace/user_avatar/community.solace.com/marc/32/2191_2.png) [@marc](https://community.solace.com/u/marc)\
**Post date:** [November 25, 2019, 4:26pm UTC](https://community.solace.com/t/messages-sent-by-spring-jmstemplate-not-being-received-by-normal-jcsmp-subscriber/115/7 "2019-11-25T16:26:20Z")

</div>

Thanks for the update Ali. Hope it’s working for you 😄  
I’m definitely curious what the issue was if you find that it was indeed the root cause.

thanks,  
Marc

---

<div class="post-metadata">

**Author:** ![alinaqvi](https://avatars.discourse-cdn.com/v4/letter/a/fbc32d/32.png) [@alinaqvi](https://community.solace.com/u/alinaqvi)\
**Post date:** [November 26, 2019, 8:59am UTC](https://community.solace.com/t/messages-sent-by-spring-jmstemplate-not-being-received-by-normal-jcsmp-subscriber/115/8 "2019-11-26T08:59:10Z")

</div>

Hey Marc,  
The receiver is basically a gui that was throwing exceptions behind the scenes because it was receiving wrong type of message.  
java.lang.ClassCastException: com.solacesystems.jcsmp.impl.XMLContentMessageImpl cannot be cast to  
com.solacesystems.jcsmp.BytesMessage

To remedy this we are now using

```
  jmsTemplate.convertAndSend("abc/topic2", msg.getBytes()); // instead of just msg which is a String

```

Apologies for the red herring post.  
Thanks,  
Ali

---

<div class="post-metadata">

**Author:** ![marc](https://yyz1.discourse-cdn.com/solace/user_avatar/community.solace.com/marc/32/2191_2.png) [@marc](https://community.solace.com/u/marc)\
**Post date:** [November 26, 2019, 2:12pm UTC](https://community.solace.com/t/messages-sent-by-spring-jmstemplate-not-being-received-by-normal-jcsmp-subscriber/115/9 "2019-11-26T14:12:59Z")

</div>

Ali,  
Excellent - glad the issue is resolved. And thanks for sharing as I’m sure this will prove to be useful to someone else as well.

-Marc
