I have this connectionfactory made with JNDI to a tibco ems server: @Bean(name =…
- User
I have this connectionfactory made with JNDI to a tibco ems server: @Bean(name = "tibJmsConnectionFactory") public ConnectionFactory tibJmsConnectionFactory() throws Exception { JndiObjectFactoryBean factoryBean = new JndiObjectFactoryBean(); factoryBean.setJndiName(env.getProperty("connectionFactoryNames")); factoryBean.setJndiEnvironment(jndiProperties()); factoryBean.afterPropertiesSet(); return (ConnectionFactory) factoryBean.getObject(); }
I need to create a method which sends and receives in the same method the message to a queue through jms of tibco ems, how can i create it inside my service class? provide me an example
- Assistant
Sure! You can create a method in your service class that sends and receives a message to a queue using JMS with Tibco EMS. Here's an example of how you can achieve this:
import javax.jms.*; @Service public class MyService { @Autowired private ConnectionFactory connectionFactory; @Value("${queueName}") private String queueName; public void sendAndReceiveMessage(String message) throws JMSException { try (Connection connection = connectionFactory.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE)) { Queue queue = session.createQueue(queueName); // Sending a message MessageProducer producer = session.createProducer(queue); TextMessage textMessage = session.createTextMessage(message); producer.send(textMessage); System.out.println("Message sent: " + message); // Receiving a message MessageConsumer consumer = session.createConsumer(queue); Message receivedMessage = consumer.receive(); if (receivedMessage instanceof TextMessage) { TextMessage textReceived = (TextMessage) receivedMessage; String received = textReceived.getText(); System.out.println("Received message: " + received); } } } } - User
use the queuerequestor
- Assistant
If you want to use the
QueueRequestorclass provided by Tibco EMS for sending and receiving messages in a single method, you can modify the code as follows:import javax.jms.*; @Service public class MyService { @Autowired private ConnectionFactory connectionFactory; @Value("${queueName}") private String queueName; public void sendAndReceiveMessage(String message) throws JMSException { try (Connection connection = connectionFactory.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE)) { Queue queue = session.createQueue(queueName); // Sending and Receiving a message with QueueRequestor connection.start(); QueueRequestor requestor = new QueueRequestor(session, queue); TextMessage requestMessage = session.createTextMessage(message); Message responseMessage = requestor.request(requestMessage); if (responseMessage instanceof TextMessage) { TextMessage textResponse = (TextMessage) responseMessage; String response = textResponse.getText(); System.out.println("Received response: " + response); } } } } - User
Caused by: java.lang.RuntimeException: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or in an application resource file: java.naming.factory.initial
- Assistant
The exception
javax.naming.NoInitialContextExceptionwith the messageNeed to specify class name in environment or system property, or in an application resource file: java.naming.factory.initialtypically occurs when the initial context factory class is not specified in the JNDI environment.To fix this issue, you need to provide the initial context factory class name in your JNDI properties or environment. Here's an example of how you can set the
java.naming.factory.initialproperty in your code:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.