Im just getting starting with ActiveMQ and i seem to have a weird problem. (Source below)
There are 2 scenarios
Consumers connect to broker, waits for tasks on the queue. Producer arrives later, drops the list of tasks and they are rightly taken up by the different consumers and performed. This works fine and i have simulated it as well.
Producer connects first, drops the list of tasks. No consumers are connected at this point. Now when lets say 3 consumers - C1, C2 and C3 connect to the broker (in that order) i see that only C1 picks up and does the tasks that are dropped by the producer. C2 and C3 stay idle. Why does this happen?
I've also noticed 1 more thing in regards to the 2nd scenario -- If the producer keeps on dropping tasks inside the queue, C2 and C3 pick up tasks but if the producer has dropped tasks before (as mentioned) then C2 and C3 dont do anything.
Producer code
package com.activemq.apps;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
import com.commons.helpers.Maths;
public class Publisher implements MessageListener {
private static String _URL;
private static String _TOPIC_PUBLISH;
private static String _TOPIC_CONSUME;
public Publisher (String URL, String TOPIC) {
_URL = URL;
_TOPIC_PUBLISH = TOPIC + "_REQUESTS";
_TOPIC_CONSUME = TOPIC + "_RESPONSES";
}
public void initialize() {
try
{
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(_URL);
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destinationProducer = session.createQueue(_TOPIC_PUBLISH);
Destination destinationConsumers = session.createQueue(_TOPIC_CONSUME);
MessageProducer producer = session.createProducer(destinationProducer);
MessageConsumer consumer = session.createConsumer(destinationConsumers);
consumer.setMessageListener(this);
int count = 0;
System.out.println("Sending requests");
while (true)
{
int randomSleepTime = Maths.rand(1000, 5000);
String messageToSend = count + "_" + randomSleepTime;
TextMessage message = session.createTextMessage(messageToSend);
producer.send(message);
System.out.println("Job #" + count + " | " + (randomSleepTime/1000) + "s");
if (count++%10 == 0)
Thread.sleep(10000);
}
}
catch (JMSException ex)
{
ex.printStackTrace();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
#Override
public void onMessage(Message message) {
if (message instanceof TextMessage)
{
TextMessage msg = (TextMessage) message;
try {
String response = msg.getText();
String[] responseSplit = response.split("_");
String clientId = responseSplit[1];
String count = responseSplit[0];
System.out.println("Got response from " + clientId + " Job #" + count);
}
catch (JMSException e) {
e.printStackTrace();
}
}
}
}
Consumer code
package com.activemq.apps;
import java.util.UUID;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
public class Consumer implements MessageListener {
private static String _URL;
private static String _TOPIC_PUBLISH;
private static String _TOPIC_CONSUME;
private static String _CLIENTID;
private MessageProducer producer;
private Session session;
public Consumer (String URL, String TOPIC) {
_URL = URL;
_TOPIC_PUBLISH = TOPIC + "_RESPONSES";
_TOPIC_CONSUME = TOPIC + "_REQUESTS";
}
public void initialize() {
try
{
_CLIENTID = UUID.randomUUID().toString();
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(_URL);
Connection connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destinationProducer = session.createQueue(_TOPIC_PUBLISH);
Destination destinationConsumers = session.createQueue(_TOPIC_CONSUME);
producer = session.createProducer(destinationProducer);
MessageConsumer consumer = session.createConsumer(destinationConsumers);
consumer.setMessageListener(this);
System.out.println("Client: " + _CLIENTID + "\nWaiting to pick up tasks");
}
catch (JMSException ex)
{
ex.printStackTrace();
}
}
#Override
public void onMessage(Message message) {
if (message instanceof TextMessage)
{
TextMessage msg = (TextMessage) message;
try
{
String[] messageSplits = msg.getText().split("_");
String count = messageSplits[0];
String timeString = messageSplits[1];
int sleepFor = Integer.parseInt(timeString);
System.out.println("Job #" + count + " | Sleeping for " + (sleepFor/1000) + "s");
Thread.sleep(sleepFor);
TextMessage sendToProducer = session.createTextMessage(count + "_" + _CLIENTID);
producer.send(sendToProducer);
}
catch (JMSException e) {
e.printStackTrace();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
You have mentioned
Now when lets say 3 consumers - C1, C2 and C3 connect to the broker
(in that order)
Since C1 connects first it starts getting all the messages on the queue immediately after it connects. That is expected. So i dont see any issue here. C2, C3 are not idle but C1 has got hold of the messages before C2 and C3 could.
I am not sure how many messages were sent by the producer. I assume the number of messages are less. To see what you expect try sending many messages from the producer, like thousands or millions and then start the consumers. The high number of messages are subjective and depends on the memory, network and other resources. You might see what you are expecting.
I dont think there is anything weird here. Queues represent P2P mode which is supposed to have only one consumer. In our case we have 3 consumers, it's not forbidden, but there is no guarantee of any specific order in which consumers will receive messages.
I believe you should check prefetchPolicy param for your consumer. By default, prefetch policy has default value 1000. It means that the first consumer fetches up to 1000 messages and other consumers are not able to fetch anything else. If you are trying load balance your messages across consumers consider to modify this param to 0 or 1.
Related
I want to execute a Kafka producer using multiple threads. Below is the code that I have tried out. I am unaware of how to implement threads in Kafka producer since I am not well versed with Thread programming.
Below is the code for my producer.
import org.apache.kafka.clients.producer.Callback;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;
import org.apache.kafka.common.Metric;
import org.apache.kafka.common.MetricName;
import org.apache.kafka.common.serialization.StringSerializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class KafkaProducerWithThread {
//init params
final String bootstrapServer = "127.0.0.1:9092";
final String topicName = "spark-data-topic";
final String csvFileName = "unique_products.csv";
final static int MAX_THREAD = 2; //created number of threads
//Logger
final Logger logger = LoggerFactory.getLogger(KafkaProducerWithThread.class);
public KafkaProducerWithThread() throws FileNotFoundException {
}
public static void main(String[] args) throws IOException {
new KafkaProducerWithThread().runProducer();
}
public void runProducer() throws IOException {
//Read the CSV file from Resources folder as BufferedReader
ClassLoader classLoader = new KafkaProducerWithThread().getClass().getClassLoader();
BufferedReader reader = new BufferedReader(new FileReader(classLoader.getResource(csvFileName).getFile()));
//Create a Kafka Producer
org.apache.kafka.clients.producer.KafkaProducer<String, String> producer = createKafkaProducer();
//Kafka Producer Metrics
Metric requestTotalMetric = null;
for (Map.Entry<MetricName, ? extends Metric> entry : producer.metrics().entrySet()) {
if ("request-total".equals(entry.getKey().name())) {
requestTotalMetric = entry.getValue();
}
}
//Thread
ExecutorService executorService = Executors.newFixedThreadPool(MAX_THREAD);
//Read the CSV file line by line
String line = "";
int i = 0;
while ((line = reader.readLine()) != null) {
i++;
String key = "products_" + i;
//Create a ProducerRecord
ProducerRecord<String, String> csvProducerRecord = new ProducerRecord<>(topicName, key, line.trim());
//Send the data - Asynchronously
producer.send(csvProducerRecord, new Callback() {
#Override
public void onCompletion(RecordMetadata recordMetadata, Exception e) {
//executes every time a record is sent successfully or an exception is thrown
if (e == null) {
//the record was sent successfully
// logger.info("Received new metadata. \n" +
// "Topic: " + recordMetadata.topic() + "\n" +
// "Partition: " + recordMetadata.partition() + "\n" +
// "Offset: " + recordMetadata.offset() + "\n" +
// "Timestamp: " + recordMetadata.timestamp());
} else {
logger.error("Error while producing", e);
}
}
});
if (i % 1000 == 0){
logger.info("Record #: " + i + " Request rate: " + requestTotalMetric.metricValue());
}
}
//Adding a shutdown hook
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
logger.info("Stopping the Producer!");
producer.flush();
producer.close();
logger.info("Stopped the Producer!");
}));
}
public org.apache.kafka.clients.producer.KafkaProducer<String, String> createKafkaProducer() {
//Create Producer Properties
Properties properties = new Properties();
properties.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServer);
properties.setProperty(ProducerConfig.ACKS_CONFIG, "all");
properties.setProperty(ProducerConfig.RETRIES_CONFIG, "5");
properties.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
properties.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
properties.setProperty(ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG, "true"); // For an idempotent producer
//kafka can detect whether it's a duplicate data based on the producer request id.
//Create high throughput Producer at the expense of latency & CPU
properties.setProperty(ProducerConfig.COMPRESSION_TYPE_CONFIG, "snappy");
properties.setProperty(ProducerConfig.LINGER_MS_CONFIG, "60");
properties.setProperty(ProducerConfig.BATCH_SIZE_CONFIG, Integer.toString(32 * 1024)); //32KB batch size
//Create Kafka Producer
org.apache.kafka.clients.producer.KafkaProducer<String, String> csvProducer = new org.apache.kafka.clients.producer.KafkaProducer<String, String>(properties);
return csvProducer;
}
}
Can anyone help me in implementing the threads in my Kafka producer program?
My Producer will be producing over a million records & so I want to implement threads for the same. I am aware of ExecutorService used for thread programming but I am not sure how to implement in this case.
Thanks.
create a MessageSender class as given below.
after creating the producer class, create a new MesssageSender object taking the producer record and producer as constructor args.
invoke executorService.submit() to perform the task.
class Producer {
ExecutorService executorService =
Executors.newFixedThreadPool(MAX_THREAD);
//Read the CSV file line by line
String line = "";
int i = 0;
while ((line = reader.readLine()) != null) {
//create produver record
ProducerRecord<String, String> csvProducerRecord = new ProducerRecord<>(topicName, key, line.trim());
MessageSender sendMessage= new MessageSender(csvProducerRecord,producer);
executorService.submit()...
}
}
//Thread class
class MessageSender implements Runnable<>{
MessageSender(Producerrecord,producer{
//store in class level variable in thread class
}
public void run(){
producer.send(csvProducerRecord...);
}
I'm a newbie in IBM MQ (https://www.ibm.com/cloud-computing/products/cloud-integration/mq-trial/index.html)
I'd like to know if I could send and get messages from IBM MQ using some code like this:
/*
* Sample program
* © COPYRIGHT International Business Machines Corp. 2009
* All Rights Reserved * Licensed Materials - Property of IBM
*
* This sample program is provided AS IS and may be used, executed,
* copied and modified without royalty payment by customer
*
* (a) for its own instruction and study,
* (b) in order to develop applications designed to run with an IBM
* WebSphere product for the customer's own internal use.
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import com.ibm.websphere.sib.api.jms.JmsConnectionFactory;
import com.ibm.websphere.sib.api.jms.JmsFactoryFactory;
import com.ibm.websphere.sib.api.jms.JmsQueue;
import com.ibm.websphere.sib.api.jms.JmsTopic;
/**
* Sample code to programmatically create a connection to a bus and
* send a text message.
*
* Example command lines:
* SIBusSender topic://my/topic?topicSpace=Default.Topic.Space MyBus localhost:7276
* SIBusSender queue://myQueue MyBus localhost:7286:BootstrapSecureMessaging InboundSecureMessaging
*/
public class SIBusSender {
/**
* #param args DEST_URL,BUS_NAME,PROVIDER_ENDPOINTS,[TRANSPORT_CHAIN]
*/
public static void main(String[] args) throws JMSException, IOException {
// Parse the arguments
if (args.length < 3) {
throw new IllegalArgumentException(
"Usage: SIBusSender <DEST_URL> <BUS_NAME> <PROVIDER_ENDPOINTS> [TARGET_TRANSPORT_CHAIN]");
}
String destUrl = args[0];
String busName = args[1];
String providerEndpoints = args[2];
String targetTransportChain = "InboundBasicMessaging";
if (args.length >= 4) targetTransportChain = args[3];
// Obtain the factory factory
JmsFactoryFactory jmsFact = JmsFactoryFactory.getInstance();
// Create a JMS destination
Destination dest;
if (destUrl.startsWith("topic://")) {
JmsTopic topic = jmsFact.createTopic(destUrl);
// Setter methods could be called here to configure the topic
dest = topic ;
}
else {
JmsQueue queue = jmsFact.createQueue(destUrl);
// Setter methods could be called here to configure the queue
dest = queue;
}
// Create a unified JMS connection factory
JmsConnectionFactory connFact = jmsFact.createConnectionFactory();
// Configure the connection factory
connFact.setBusName(busName);
connFact.setProviderEndpoints(providerEndpoints);
connFact.setTargetTransportChain(targetTransportChain);
// Create the connection
Connection conn = connFact.createConnection();
Session session = null;
MessageProducer producer = null;
try {
// Create a session
session = conn.createSession(false, // Not transactional
Session.AUTO_ACKNOWLEDGE);
// Create a message producer
producer = session.createProducer(dest);
// Loop reading lines of text from the console to send
System.out.println("Ready to send to " + dest + " on bus " + busName);
BufferedReader lineInput = new BufferedReader(new InputStreamReader(System.in));
String line = lineInput.readLine();
while (line != null && line.length() > 0) {
// Create a text message containing the line
TextMessage message = session.createTextMessage();
message.setText(line);
// Send the message
producer.send(message,
Message.DEFAULT_DELIVERY_MODE,
Message.DEFAULT_PRIORITY,
Message.DEFAULT_TIME_TO_LIVE);
// Read the next line
line = lineInput.readLine();
}
}
// Finally block to ensure we close our JMS objects
finally {
// Close the message producer
try {
if (producer != null) producer.close();
}
catch (JMSException e) {
System.err.println("Failed to close message producer: " + e);
}
// Close the session
try {
if (session != null) session.close();
}
catch (JMSException e) {
System.err.println("Failed to close session: " + e);
}
// Close the connection
try {
conn.close();
}
catch (JMSException e) {
System.err.println("Failed to close connection: " + e);
}
}
}
}
I created the queue in the IBM MQ Explorer.
My main doubt is how to do the connection and go on without application server.
I'm trying to UNSUBSCRIBE durable subscribers from TOPICS.
My app is a kind of social network : each user is a topic for other users. So, each time a user is doing something, his friends are notified. Of course, a subscriber may unsubscribe from a topic, wanting to receive notifications about a user no more.
Each time I'm trying to unsubscribe a subscriber from a topic, I've got an error telling me that : "javax.jms.JMSException: Durable consumer is in use"
Here are my 2 classes, the SENDER one and the RECEIVER one. Can someone tell me what I'm doing wrong ??
SENDER Class :
package com.citizenweb.classes;
import java.util.Date;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageFormatException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.ObjectMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ActiveMQSession;
import com.citizenweb.interfaces.PostIF;
import com.citizenweb.interfaces.UserIF;
public class Sender {
private ActiveMQConnectionFactory factory = null;
private ActiveMQConnection connection = null;
private ActiveMQSession session = null;
private Destination destination = null;
private MessageProducer producer = null;
public Sender() {
}
public void connect(){
try{
factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_BROKER_URL);
// TODO Mécanisme de sécurité d'ActiveMQ à rétablir en production
factory.setTrustAllPackages(true);
connection = (ActiveMQConnection) factory.createConnection();
connection.start();
session = (ActiveMQSession) connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
} catch (JMSException e){
e.printStackTrace();
}
}
public void sendPost(UserIF user,PostIF post) {
if(session==null){connect();}
try {
destination = session.createTopic(user.toString());
producer = session.createProducer(destination);
ObjectMessage postMessage = session.createObjectMessage();
postMessage.setObject(post);
producer.send(postMessage);
System.out.println("\n SENDER Object message sent");
} catch (MessageFormatException e) {
e.printStackTrace();
} catch (JMSException e) {
e.printStackTrace();
}
}
public void sendInformation(UserIF user,String info){
if(session==null){connect();}
try {
destination = session.createTopic(user.toString());
producer = session.createProducer(destination);
TextMessage infoMessage = session.createTextMessage();
infoMessage.setText(info);
producer.send(infoMessage);
System.out.println("\n SENDER Information message sent");
} catch (JMSException e) {
e.printStackTrace();
}
}
/**
* #param args
* #throws Exception
*/
public static void main(String[] args) throws Exception {
UserIF u1, u2, u3;
String[] nom = new String[5];
String[] prenom = new String[5];
String[] login = new String[5];
String[] password = new String[5];
Date[] naiss = new Date[5];
String[] mail = new String[5];
for (int i = 0; i < 5; i++) {
nom[i] = "nom_" + i;
prenom[i] = "prenom_" + i;
login[i] = "login_" + i;
password[i] = "password_" + i;
naiss[i] = new Date();
mail[i] = "mail_" + i;
}
System.out.println("\n SENDER AFFECTATION DES NOMS");
u1 = new User(nom[0], prenom[0], login[0], password[0], naiss[0], mail[0]);
u2 = new User(nom[1], prenom[1], login[1], password[1], naiss[1], mail[1]);
u3 = new User(nom[2], prenom[2], login[2], password[2], naiss[2], mail[2]);
Sender sender = new Sender();
sender.sendInformation(u1, "U1 notification");
sender.sendInformation(u2, "U2 notification");
sender.sendInformation(u3, "U3 notification");
//PostIF post = new Post("Mon Post","Ceci est mon message",u1,u1,"Classe Sender",((User) u1).getIdUser(),0);
//sender.sendPost(user, post);
sender.session.close();
sender.connection.close();
}
}
RECEIVER Class :
package com.citizenweb.classes;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ActiveMQSession;
import org.apache.activemq.broker.region.Destination;
import com.citizenweb.interfaces.PostIF;
import com.citizenweb.interfaces.UserIF;
import com.citizenweb.classes.Post;
public class Receiver implements MessageListener, Serializable {
private static final long serialVersionUID = 1L;
private ActiveMQConnectionFactory factory = null;
private ActiveMQConnection connection = null;
private ActiveMQSession session = null;
private Topic destination = null;
private MessageConsumer consumer = null;
UserIF userTopic = new User();
UserIF userSubscriber = new User();
List<Message> listeMsg = new ArrayList<Message>();
public Receiver(UserIF subscriber) {
this.userSubscriber = subscriber;
}
public void connect() {
try {
factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_BROKER_URL);
// TODO Mécanisme de sécurité d'ActiveMQ à rétablir en production
factory.setTrustAllPackages(true);
connection = (ActiveMQConnection) factory.createConnection();
// ClientID :
// https://qnalist.com/questions/2068823/create-durable-topic-subscriber
connection.setClientID(userSubscriber.toString());
connection.start();
session = (ActiveMQSession) connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
} catch (JMSException e) {
e.printStackTrace();
}
}
public void receiveMessage(UserIF topic) {
try {
if (session == null) {
connect();
}
destination = session.createTopic(topic.toString());
String nomAbonnement = topic.toString() + "->" + userSubscriber.toString();
//String nomAbonnement = userSubscriber.toString();
consumer = session.createDurableSubscriber(destination, nomAbonnement);
consumer.setMessageListener(this);
} catch (JMSException e) {
e.printStackTrace();
}
}
public void unsubscribe(UserIF topic) {
try {
if (session == null) {
connect();
}
System.out.println("\n RECEIVER Désinscription du topic " + topic.toString());
//consumer.close();
String nomAbonnement = topic.toString() + "->" + userSubscriber.toString();
//String nomAbonnement = userSubscriber.toString();
System.out.println("\n RECEIVER Abonnement à clore = " + nomAbonnement);
session.unsubscribe(nomAbonnement);
System.out.println("\n RECEIVER " + userSubscriber.toString() + " s'est désinscrit de " + nomAbonnement);
} catch (JMSException e) {
e.printStackTrace();
}
}
#Override
public void onMessage(Message message) {
System.out.println("\n RECEIVER OnMessage triggered for " + userSubscriber.toString());
listeMsg.add(message);
System.out.println("\n RECEIVER Nombre de messages reçus par " + userSubscriber + " = " + listeMsg.size());
String classe = message.getClass().getSimpleName();
System.out.println("\n RECEIVER Classe de message : " + classe);
try {
if (message instanceof TextMessage) {
TextMessage text = (TextMessage) message;
System.out.println("\n RECEIVER Information : " + text.getText());
}
if (message instanceof ObjectMessage) {
System.out.println("\n RECEIVER ObjectMessage");
ObjectMessage oMessage = (ObjectMessage) message;
if (oMessage.getObject() instanceof PostIF) {
PostIF post = (PostIF) oMessage.getObject();
String s = ((Post) post).getCorpsMessage();
System.out.println("\n RECEIVER Post : " + s);
}
}
} catch (JMSException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws JMSException {
/*
* EACH USER IS A TOPIC FOR OTHER USERS
* WHATEVER A USER DOES RESULTS IN A NOTIFICATION TO SUBSCRIBERS
*/
//CREATE USER
UserIF u1, u2, u3;
String[] nom = new String[5];
String[] prenom = new String[5];
String[] login = new String[5];
String[] password = new String[5];
Date[] naiss = new Date[5];
String[] mail = new String[5];
for (int i = 0; i < 5; i++) {
nom[i] = "nom_" + i;
prenom[i] = "prenom_" + i;
login[i] = "login_" + i;
password[i] = "password_" + i;
naiss[i] = new Date();
mail[i] = "mail_" + i;
}
u1 = new User(nom[0], prenom[0], login[0], password[0], naiss[0], mail[0]);
u2 = new User(nom[1], prenom[1], login[1], password[1], naiss[1], mail[1]);
u3 = new User(nom[2], prenom[2], login[2], password[2], naiss[2], mail[2]);
/*
* MAKE EACH USER A SUBSCRIBER
*/
Receiver receiver1 = new Receiver(u1);
Receiver receiver2 = new Receiver(u2);
Receiver receiver3 = new Receiver(u3);
/*
* PUT A MESSAGE LISTENER FOR EACH USER
*/
receiver1.receiveMessage(u2);
receiver1.receiveMessage(u3);
receiver2.receiveMessage(u1);
receiver2.receiveMessage(u3);
receiver3.receiveMessage(u1);
receiver3.receiveMessage(u2);
/*
* CALL THE SENDER CLASS TO SEND MESSAGES
*/
try {
Sender.main(args);
} catch (Exception e1) {
e1.printStackTrace();
}
/*
* A SLEEP TO HAVE ENOUGH TIME TO LOOK AT THE ACTIVEMQ CONSOLE
* CAN BE REMOVE
*/
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
/*
* UNSUBSCRIBE SUBSCRIBERS FROM TOPICS
*/
receiver1.unsubscribe(u2);
receiver1.unsubscribe(u3);
receiver2.unsubscribe(u1);
receiver2.unsubscribe(u3);
receiver3.unsubscribe(u1);
receiver3.unsubscribe(u2);
}
}
Each connection needs a unique ClientID : connection.setClientID("clientID");
My mistake was to misunderstand this unicity for a given client.
When a client subscribes to a Topic, there is one connection for this Topic. So, for a given client subscribed to 3 topics (for instance), 3 ClientID are needed because 3 connections are needed.
A ClientID has to be unique because it identifies one connection of one client for one topic.
That's why I had so many JMSException telling that "Durable Consumer was In Use" when I wanted to end its subscription.
You can only unsubscribe a durable subscription if there is no active subscriber currently consuming from it. It looks like your code creates several subscriptions and does not stop the consumers so of course the unsubscribe will fail, if you close down the consumers and then do the unsubscribe you should get the result you are looking for.
An example of durable subscription unsubscribe is here.
I've a Queue in Wildfly 8.2.0 and deployed ear with listener. Then i'm trying to post Text Message to Remote Queue from Client program. In this scenario, unable to lookup Remote Connection Factory. Herewith attaching the program and error.
MDB Listener on Wildfly
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
#MessageDriven(activationConfig = {
#ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
#ActivationConfigProperty(propertyName = "destination", propertyValue = "jms/queue/mdbtest")})
public class ExampleQueueMDB implements MessageListener {
#Override
public void onMessage(Message message) {
if (message instanceof TextMessage) {
try {
System.out.println("----------------");
System.out.println("Received: "
+ ((TextMessage) message).getText());
System.out.println("----------------");
} catch (JMSException e) {
e.printStackTrace();
}
} else {
System.out.println("----------------");
System.out.println("Received: " + message);
System.out.println("----------------");
}
}
}
Java Client Program
import java.util.logging.Logger;
import java.util.Properties;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
public class HelloWorldJMSClientSample {
private static final Logger log = Logger.getLogger(HelloWorldJMSClient.class.getName());
// Set up all the default values
private static final String DEFAULT_MESSAGE = "Hello, World!";
private static final String DEFAULT_CONNECTION_FACTORY = "jms/RemoteConnectionFactory";
private static final String DEFAULT_DESTINATION = "jms/queue/mdbtest";
private static final String DEFAULT_MESSAGE_COUNT = "1";
private static final String DEFAULT_USERNAME = "user";
private static final String DEFAULT_PASSWORD = "pass";
private static final String INITIAL_CONTEXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory";
//private static final String PROVIDER_URL = "http-remoting://localhost:8080";
private static final String PROVIDER_URL = "http-remoting://localhost:8080";
public static void main(String[] args) throws Exception {
ConnectionFactory connectionFactory = null;
Connection connection = null;
Session session = null;
MessageProducer producer = null;
MessageConsumer consumer = null;
Destination destination = null;
TextMessage message = null;
Context context = null;
try {
// Set up the context for the JNDI lookup
final Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
env.put(Context.PROVIDER_URL, PROVIDER_URL);
env.put(Context.SECURITY_PRINCIPAL, DEFAULT_USERNAME);
env.put(Context.SECURITY_CREDENTIALS, DEFAULT_PASSWORD);
env.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
context = new InitialContext(env);
// Perform the JNDI lookups
String connectionFactoryString = System.getProperty("connection.factory", DEFAULT_CONNECTION_FACTORY);
log.info("Attempting to acquire connection factory \"" + connectionFactoryString + "\"");
connectionFactory = (ConnectionFactory) context.lookup(connectionFactoryString);
log.info("Found connection factory \"" + connectionFactoryString + "\" in JNDI");
String destinationString = System.getProperty("destination", DEFAULT_DESTINATION);
log.info("Attempting to acquire destination \"" + destinationString + "\"");
destination = (Destination) context.lookup(destinationString);
log.info("Found destination \"" + destinationString + "\" in JNDI");
// Create the JMS connection, session, producer, and consumer
connection = connectionFactory.createConnection(DEFAULT_USERNAME,DEFAULT_PASSWORD);
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
producer = session.createProducer(destination);
// consumer = session.createConsumer(destination);
connection.start();
int count = Integer.parseInt(System.getProperty("message.count", DEFAULT_MESSAGE_COUNT));
String content = System.getProperty("message.content", DEFAULT_MESSAGE);
log.info("Sending " + count + " messages with content: " + content);
// Send the specified number of messages
for (int i = 0; i < count; i++) {
message = session.createTextMessage(content);
producer.send(message);
}
} catch (Exception e) {
log.severe(e.getMessage());
throw e;
} finally {
if (context != null) {
context.close();
}
// closing the connection takes care of the session, producer, and consumer
if (connection != null) {
connection.close();
}
}
}
}
ERROR
SEVERE: Failed to create session factory
Exception in thread "main" javax.jms.JMSException: Failed to create session factory
at org.hornetq.jms.client.HornetQConnectionFactory.createConnectionInternal(HornetQConnectionFactory.java:615)
at org.hornetq.jms.client.HornetQConnectionFactory.createConnection(HornetQConnectionFactory.java:121)
at HelloWorldJMSClientSample.main(HelloWorldJMSClientSample.java:62)
Caused by: java.lang.IllegalStateException: The following keys are invalid for configuring a connector: http-upgrade-endpoint, http-upgrade-enabled
at org.hornetq.core.client.impl.ClientSessionFactoryImpl.checkTransportKeys(ClientSessionFactoryImpl.java:1227)
at org.hornetq.core.client.impl.ClientSessionFactoryImpl.<init>(ClientSessionFactoryImpl.java:181)
at org.hornetq.core.client.impl.ServerLocatorImpl.createSessionFactory(ServerLocatorImpl.java:589)
at org.hornetq.jms.client.HornetQConnectionFactory.createConnectionInternal(HornetQConnectionFactory.java:611)
... 2 more
Above error came while creating connection on line 62,
connection = connectionFactory.createConnection(DEFAULT_USERNAME,DEFAULT_PASSWORD);
I'm not sure, why this was not working. Hope this is a straight forward case. Please let me know, if there is different way to lookup RemoteConnectionFactory. Thanks.
Update:
Method 2:
If i use below configuration, able to call any ejb methods but still throwing error for RemoteConnectionFactory lookup.
// Set up the context for the JNDI lookup
final Properties env = new Properties();
env.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
env.put("endpoint.name", "client-endpoint");
env.put("remote.connections","default");
env.put("remote.connection.default.port","8080");
env.put("remote.connection.default.host","localhost");
env.put("remote.connection.default.username", DEFAULT_USERNAME);
env.put("remote.connection.default.password", DEFAULT_PASSWORD);
env.put("org.jboss.ejb.client.scoped.context",true);
context = new InitialContext(env);
Error after above change,
INFO: Attempting to acquire connection factory "jms/RemoteConnectionFactory"
SEVERE: Receive timed out
Exception in thread "main" javax.naming.CommunicationException: Receive timed out [Root exception is java.net.SocketTimeoutException: Receive timed out]
at org.jnp.interfaces.NamingContext.discoverServer(NamingContext.java:1317)
at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1446)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:594)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:587)
at javax.naming.InitialContext.lookup(InitialContext.java:411)
at HelloWorldJMSClientSample.main(HelloWorldJMSClientSample.java:56)
Caused by: java.net.SocketTimeoutException: Receive timed out
at java.net.PlainDatagramSocketImpl.receive0(Native Method)
at java.net.AbstractPlainDatagramSocketImpl.receive(AbstractPlainDatagramSocketImpl.java:146)
at java.net.DatagramSocket.receive(DatagramSocket.java:816)
at org.jnp.interfaces.NamingContext.discoverServer(NamingContext.java:1287)
... 5 more
Error came on lookup, line 56,
connectionFactory = (ConnectionFactory) context.lookup(connectionFactoryString);
Please provide any solution for this problem. Thanks.
Found answer from https://developer.jboss.org/thread/265919.
Since i'm working with infinispan, i've used the infinispan version(6.0.2) of Wildfly 8.2.0 for client program. But Wildfly using HornetQ 2.4.5 and client having HornetQ 2.2.7. I've changed to HornetQ 2.4.5 on client side and everything working fine.
I am new to JMS, after a long re search i googled out a code to connect to JMS and post a msg.
The problem is i need to post the message in a remote queue, But i am not sure how to establish connection to it and post the message.
SERVER TYPE : TIBCO EMS
SERVER HOST : **.*****.net
PORT : *
**USername : user
passsword : user123
Queue : *.*....Order.Management..1
I would like to establish connection, post a simple msg and retrieve it back.
Kindly help! thanks in advance
CODE WHICH I GOT FROM INTERNET
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Hashtable;
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class emm {
// Defines the JNDI context factory.
public final static String JNDI_FACTORY="com.tibco.tibjms.naming.TibjmsInitialContextFactory";
// Defines the JMS context factory.
public final static String JMS_FACTORY="jms/TestConnectionFactory";
// Defines the queue.
public final static String QUEUE="CPW.GBR.POR.Public.Request.Order.Management.UpdateProvisioningStatus.1";
private QueueConnectionFactory qconFactory;
private ConnectionFactory conFactory;
private QueueConnection qcon;
private QueueSession qsession;
private QueueSender qsender;
private Queue queue;
private TextMessage msg;
/**
* Creates all the necessary objects for sending
* messages to a JMS queue.
*
* #param ctx JNDI initial context
* #param queueName name of queue
* #exception NamingException if operation cannot be performed
* #exception JMSException if JMS fails to initialize due to internal error
*/
public void init(Context ctx, String queueName)
throws NamingException, JMSException
{
qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
qcon = qconFactory.createQueueConnection();
qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queue = (Queue) ctx.lookup(queueName);
qsender = qsession.createSender(queue);
msg = qsession.createTextMessage();
qcon.start();
}
/**
* Sends a message to a JMS queue.
*
* #param message message to be sent
* #exception JMSException if JMS fails to send message due to internal error
*/
public void send(String message) throws JMSException {
msg.setText(message);
qsender.send(msg);
}
/**
* Closes JMS objects.
* #exception JMSException if JMS fails to close objects due to internal error
*/
public void close() throws JMSException {
qsender.close();
qsession.close();
qcon.close();
}
/** main() method.
*
* #param args WebLogic Server URL
* #exception Exception if operation fails
*/
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.out.println("Usage: java examples.jms.queue.QueueSend WebLogicURL");
return;
}
InitialContext ic = getInitialContext(args[0]);
emm qs = new emm();
qs.init(ic, QUEUE);
readAndSend(qs);
qs.close();
}
private static void readAndSend(emm qs)
throws IOException, JMSException
{
BufferedReader msgStream = new BufferedReader(new InputStreamReader(System.in));
String line=null;
boolean quitNow = false;
do {
System.out.print("Enter message (\"quit\" to quit): \n");
line = msgStream.readLine();
if (line != null && line.trim().length() != 0) {
qs.send(line);
System.out.println("JMS Message Sent: "+line+"\n");
quitNow = line.equalsIgnoreCase("quit");
}
} while (! quitNow);
}
private static InitialContext getInitialContext(String url)
throws NamingException
{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, url);
return new InitialContext(env);
}
}
Finally from different sources i found the best possible to way for this.
This code will definitely work. I have tried this out and is running currently on my machine
import java.util.Properties;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.*;
public class JMSExample {
static String serverUrl = "tcp://10.101.111.101:10001"; // values changed
static String userName = "user";
static String password = "pwd";
static QueueConnection connection;
static QueueReceiver queueReceiver;
static Queue queue;
static TextMessage message;
public static void sendTopicMessage(String topicName, String messageStr) {
Connection connection = null;
Session session = null;
MessageProducer msgProducer = null;
Destination destination = null;
try {
TextMessage msg;
System.out.println("Publishing to destination '" + topicName
+ "'\n");
ConnectionFactory factory = new com.tibco.tibjms.TibjmsConnectionFactory(
serverUrl);
connection = factory.createConnection(userName, password);
session = connection.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue(topicName);
msgProducer = session.createProducer(null);
msg = session.createTextMessage();
msg.setStringProperty("SourceId", userName);
msg.setStringProperty("BusinessEvent", password);
msg.setText(messageStr);
msgProducer.send(destination, msg);
System.out.println("Published message: " + messageStr);
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws JMSException {
// TODO Auto-generated method stub
JMSExample.sendTopicMessage("***.***.***.**.**.Order.Management.***.1",
"Hi");
//System.out.println(getMessage());
}
As i mentioned some code for inspiration (SLIGHTLY improved your code. Not something i would want to see in production!)
// Use the domain-agnostic API
private Connection connection;ery
private Session session;
private MessageProducer producer;
private Queue queue;
public void init(Context ctx, String queueName) {
try {
ConnectionFactory cnf = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
queue = (Queue) ctx.lookup(queueName);
connection = cnf.createConnection("user", "user123");
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
producer = session.createProducer(queue);
connection.start();
} catch (NamingException e) {
throw new RuntimeException(e);
} catch (JMSException e) {
throw new RuntimeException(e);
}
}
And make your send like this (don't reuse the same Message object, but create a new one for each message you are sending)
public void send(String message) throws JMSException {
TextMessage msg = session.createTextMessage();
msg.setText(message);
producer.send(msg);
}
Put a try..finally around the code in your main method:
try {
readAndSend(qs);
} finally {
qs.close();
}
The code you are using is not very good (understatement). It is programmed too brittle for a production system. You should not use state the way this program does.
Also there is no need to use the domain specific JMS API. You can use the domain (queue/topic) agnostic one.
When you run the program pass in the JNDI URL for your TIBCO server.