I have given the received message length as 1000000 but still message gets truncated completed code is below
import java.io.FileInputStream;
import javax.jms.JMSException;
import javax.jms.Session;
import com.ibm.jms.JMSMessage;
import com.ibm.jms.JMSTextMessage;
import com.ibm.mq.jms.JMSC;
import com.ibm.mq.jms.MQQueue;
import com.ibm.mq.jms.MQQueueConnection;
import com.ibm.mq.jms.MQQueueConnectionFactory;
import com.ibm.mq.jms.MQQueueReceiver;
import com.ibm.mq.jms.MQQueueSender;
import com.ibm.mq.jms.MQQueueSession;
public class SimplePTP {
public static void main(String[] args) {
try {
MQQueueConnectionFactory cf = new MQQueueConnectionFactory();
String request= null;
// Config
cf.setHostName("CTMQ9000");
cf.setPort(1414);
cf.setTransportType(JMSC.MQJMS_TP_CLIENT_MQ_TCPIP);
cf.setQueueManager("CTMQTST01");
cf.setChannel("SYSTEM.ADMIN.SVRCONN");
MQQueueConnection connection = (MQQueueConnection) cf.createQueueConnection();
MQQueueSession session = (MQQueueSession) connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
MQQueue queue = (MQQueue) session.createQueue("CONTPLAT.CPS.DELIVERYPREP.REQUEST.TEST");
MQQueueSender sender = (MQQueueSender) session.createSender(queue);
MQQueue queue1 = (MQQueue) session.createQueue("CONTPLAT.CPS.DELIVERYPREP.RESPONSE.TEST");
MQQueueReceiver receiver = (MQQueueReceiver) session.createReceiver(queue1);
String request ="sdfHelp Me Name name for Photo Studio!I'm opening a portrait studio in a my town and am stuck on what to name it. I will be photographing (Portrait, wedding Photography)) children and families both at the studio and on location.sdffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffsdf"
long uniqueNumber = System.currentTimeMillis() % 1000;
JMSTextMessage message = (JMSTextMessage) session.createTextMessage(request);
// Start the connection
connection.start();
sender.send(message);
System.out.println("Sent message:\\n" + message +"lol");
Thread.sleep(2000);
JMSMessage receivedMessage = (JMSMessage) receiver.receive(10000000);
System.out.println("\\nReceived message:\\n" + receivedMessage);
sender.close();
receiver.close();
session.close();
connection.close();
System.out.println("\\nSUCCESS\\n");
}
catch (JMSException jmsex) {
System.out.println(jmsex);
System.out.println("\\nFAILURE\\n");
}
catch (Exception ex) {
System.out.println(ex);
System.out.println("\\nFAILURE\\n");
}
}
}
If the length of the message exceeds some limit i don't know how much , but its getting truncated , any way to increase or workaround to display complete message.
As Germann pointed out the parameter that you pass to receive method is not the message size, it is actually the wait time in milli seconds. Meaning how long the call must wait for a message to arrive. For example if you have set the wait time as 1000, then the receive call will wait for 1 second for message to arrive. If a message arrives before 1 second, the call will return immediately and give the message to application. If a message does not arrive even after a second, then the call will return with a timeout and no message is given to application. In MQ terms you will see a 2033 reason code.
How are you determining that the message is truncated? Are you getting a MQRC_TRUNCATED_MESSAGE_FAILED exception? This exception will be thrown if the application supplied buffer is not enough to fill the incoming message. MQ JMS is not expected to throw a MQRC_TRUNCATED_MESSAGE_FAILED exception as it internally handles the buffer size required and returns message to application.
I am guessing that the issue could be because you are printing a JMSMessage whereas the sent message is a JMSTextMessage. JMSMessage.ToString may not be printing the whole message.
Where do you set the length to 1000000?
If you read MQ docs you will find that receiver.receive(10000000); does NOT set the message size.
Related
I need to send my messages to Dead letter queue from azure topic subscription incase of any error while reading and processing the message from topic. So I tried testing pushing message directly to DLQ.
My sample code will be like
static void sendMessage()
{
// create a Service Bus Sender client for the queue
ServiceBusSenderClient senderClient = new ServiceBusClientBuilder()
.connectionString(connectionString)
.sender()
.topicName(topicName)
.buildClient();
// send one message to the topic
senderClient.sendMessage(new ServiceBusMessage("Hello, World!"));
}
static void resceiveAsync() {
ServiceBusReceiverAsyncClient receiver = new ServiceBusClientBuilder()
.connectionString(connectionString)
.receiver()
.topicName(topicName)
.subscriptionName(subName)
.buildAsyncClient();
// receive() operation continuously fetches messages until the subscription is disposed.
// The stream is infinite, and completes when the subscription or receiver is closed.
Disposable subscription = receiver.receiveMessages().subscribe(message -> {
System.out.printf("Id: %s%n", message.getMessageId());
System.out.printf("Contents: %s%n", message.getBody().toString());
}, error -> {
System.err.println("Error occurred while receiving messages: " + error);
}, () -> {
System.out.println("Finished receiving messages.");
});
// Continue application processing. When you are finished receiving messages, dispose of the subscription.
subscription.dispose();
// When you are done using the receiver, dispose of it.
receiver.close();
}
I tried getting the deadletter queue path
String dlq = EntityNameHelper.formatDeadLetterPath(topicName);
I got path of dead letter queue like = "mytopic/$deadletterqueue"
But It's not working while passing path as topic name. It throwing a Entity topic not found exception.
Any one can you please advise me on this
Reference :
How to move error message to Azure dead letter queue using Java?
https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-dead-letter-queues#moving-messages-to-the-dlq
How to push the failure messages to Azure service bus Dead Letter Queue in Spring Boot Java?
https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-java-how-to-use-topics-subscriptions-legacy#receive-messages-from-a-subscription
You probably know that a message will be automatically moved to the deadletter queue if you throw exceptions during processing, and the maximum delievery count is exceeded. If you want to explicitly move the message to the DLQ, you can do so as well. A common case for this is if you know that the message can never succeed because of its contents.
You cannot send new messages directly to the DLQ, because then you would have two messages in the system. You need to call a special operation on the parent entity. Also, <topic path>/$deadletterqueue does not work, because this would be the DLQ of all subscriptions. The correct entity path is built like this:
<queue path>/$deadletterqueue
<topic path>/Subscriptions/<subscription path>/$deadletterqueue
https://github.com/Azure/azure-service-bus/blob/master/samples/Java/azure-servicebus/DeadletterQueue/src/main/java/com/microsoft/azure/servicebus/samples/deadletterqueue/DeadletterQueue.java
This sample code is for queues, but you should be able to adapt it to topics quite easily:
// register the RegisterMessageHandler callback
receiver.registerMessageHandler(
new IMessageHandler() {
// callback invoked when the message handler loop has obtained a message
public CompletableFuture<Void> onMessageAsync(IMessage message) {
// receives message is passed to callback
if (message.getLabel() != null &&
message.getContentType() != null &&
message.getLabel().contentEquals("Scientist") &&
message.getContentType().contentEquals("application/json")) {
// ...
} else {
return receiver.deadLetterAsync(message.getLockToken());
}
return receiver.completeAsync(message.getLockToken());
}
// callback invoked when the message handler has an exception to report
public void notifyException(Throwable throwable, ExceptionPhase exceptionPhase) {
System.out.printf(exceptionPhase + "-" + throwable.getMessage());
}
},
// 1 concurrent call, messages are auto-completed, auto-renew duration
new MessageHandlerOptions(1, false, Duration.ofMinutes(1)),
executorService);
Tried setting an IBM MQ custom property in JMS while sending messages. It doesn't work.
I'm looking for an equivalent of the following in JMS/Apache camel.
mQMessage.setStringProperty( "customProperty", "123" );
Tried the following 3 options:
1) exchange.getIn().setHeader( "customProperty", "123" );
2) exchange.getIn().setProperty( "customProperty", "123" );
3) mQQueueConnectionFactory.setStringProperty( "customProperty", "123" );
The following code to read the property throws error because the property doesn't exist it seems.
mQMessage.getStringProperty( "messageGlobalSequenceNumber" )
throws the following error:
com.ibm.mq.MQException: MQJE001: Completion Code '2', Reason '2471'.
at com.ibm.mq.MQMessage.getProperty(MQMessage.java:5694)
at com.ibm.mq.MQMessage.getStringProperty(MQMessage.java:6949)
at com.ibm.mq.MQMessage.getStringProperty(MQMessage.java:6925)
...
Are you sure that the property that you are trying to retrieve actually exists for that message? Because Reason Code of 2471 (MQRC_PROPERTY_NOT_AVAILABLE) clearly says that the named property does not exist.
The correct way to create a message property in JMS (for IBM MQ) is as follows:
/**
* Send a message to a queue.
* #param session
* #param myQ
* #throws JMSException
*/
private void sendMsg(QueueSession session, Queue myQ) throws JMSException
{
QueueSender sender = null;
try
{
TextMessage msg = session.createTextMessage();
msg.setText("This is a test message.");
msg.setStringProperty("MyProp01", "somevalue");
sender = session.createSender(myQ);
sender.send(msg);
}
finally
{
try
{
if (sender != null)
sender.close();
}
catch (Exception ex)
{
System.out.println("sender.close() : " + ex.getLocalizedMessage());
}
}
}
Did you use an MQ tool to check the property values of the message? I ran the above code then checked the message on the queue with MQ Visual Edit and here is a screen-shot:
Or a screen-shot of the opened selected message showing the Named Properties (aka message properties):
I'm successfully pushing message into ActiveMQ from local Eclipse setup. However, the same code does not push message when I try to execute from server as a cron job. It does not even throw an exception during code execution.
Java environment - 1.8
Supporting jars used:
slf4j-api-1.8.0-beta2.jar
javax.annotation-api-1.2.jar
javax.jms-api-2.0.1.jar
management-api-1.1-rev-1.jar
activemq-core-5.7.0.jar
Code:
try {
map = getMessageDetails(session,"MessageQueueEmail");
userName = map.get("userName");
password = map.get("password");
hostName = map.get("mqHostName");
queue = map.get("queueName");
// Create a ConnectionFactory
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(userName, password, hostName);
// Create a Connection
connection = factory.createConnection();
// start the Connection
connection.start();
System.out.println("MQ started connection");
// Create a Session
sessionMQ = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create the destination Queue
Destination destination = sessionMQ.createQueue(queue);
// Create a MessageProducer from the Session to the Queue
messageProducer = sessionMQ.createProducer(destination);
messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
// Create a message
Message message = sessionMQ.createTextMessage(textMsg);
System.out.println("MQ Message sent successfully");
// Tell the producer to send the message
messageProducer.send(message);
} catch(Exception e) {
e.printStackTrace();
System.out.println("\n::::::::::::Error occurred sendEmailMessageToIntranet::::::::::::: " + e.getMessage());
}
Thanks everyone for response. The issue is resolved after importing correct certificate file to the server. Wondering, why MQ attempts failure notification had not logged
Your code looks ok except you might have expiration going. Try with PERSISTENT and most likely is the issues that you are not redirecting stderr in your cronjob ? Make sure you do something like this:
*/1 * * * * /something/send.sh &>> /something/out.log
And then check in the morning.
This is probably some silly mistake I'm missing, but here is the issue:
I am trying to insert a simple "hello" message into a Rabbit queue, with a predefined exchange and routing key.
This is the code that I am using:
private static void send_equity_task_to_rabbitmq(ConnectionFactory factory) throws IOException,TimeoutException{
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare("b", false, false, false, null);
channel.exchangeDeclare("b", "direct");
channel.basicPublish("b","b",null, "hello".getBytes());
channel.close();
connection.close();
}
public static void main(String[] argv) throws TimeoutException,IOException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("127.0.0.1");
Date start_time= Calendar.getInstance().getTime();
Long start_time_timestamp=System.currentTimeMillis();
System.out.println("[INFO] Starting connection to queue at:"+start_time);
send_equity_task_to_rabbitmq(factory);
Long end_time_timestamp=System.currentTimeMillis();
System.out.println("[INFO] Message sent and processed successfully after:"+ (end_time_timestamp-start_time_timestamp)+" miliseconds");
}
}
The code runs without any error. However, when I check the amount of records inside the "b" queue, I get:
$ rabbitmqctl list_queues
Listing queues ...
b 0
...done.
I don't have consumers for this queue at the moment, so I assume since it has 0 records, that I am using basicPublish badly.
What could be wrong?
Thank you.
I think you need to bind the queue to the exchange. You've created a queue called "b" and an exchange called "b". The exchange will distribute messages to queues that are bound to it, using the "b" routingKey, but as the "b" queue isn't bound to the "b" exchange, the "b" exchange doesn't publish to that queue.
i am new to JMS and going thru the example of Active MQ Hello world. Say i have a scenario whenever i make entry
under employee table in DB, i have to put the message in queue.here is the producer code snippet from hello world example
public static class HelloWorldProducer {
public void createMessageOnQueue() {
try {
// Create a ConnectionFactory
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost");
// Create a Connection
Connection connection = connectionFactory.createConnection();
connection.start();
// Create a Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create the destination (Topic or Queue)
Destination destination = session.createQueue("TEST.FOO");
// Create a MessageProducer from the Session to the Topic or Queue
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
// Create a messages
String text = "Hello world! From: " + Thread.currentThread().getName() + " : " + this.hashCode();
TextMessage message = session.createTextMessage(text);
// Tell the producer to send the message
System.out.println("Sent message: "+ message.hashCode() + " : " + Thread.currentThread().getName());
producer.send(message);
// Clean up
session.close();
connection.close();
}
catch (Exception e) {
System.out.println("Caught: " + e);
e.printStackTrace();
}
}
}
Now my question is if i close the connection and session, will it close the queue also? If yes,what will happen if message has not been consumed yet?
Second question is if i need to publish the message on same queue(i.e "TEST.FOO") second time , do i need to call createMessageOnQueue method second time. If yes, will it not create new queue with session.createQueue("TEST.FOO")?
Now my question is if i close the connection and session, will it
close the queue also? If yes,what will happen if message has not been
consumed yet?
message will still be on queue. No such thing as 'closing a queue'.
Second question is if i need to publish the message on same queue(i.e
"TEST.FOO") second time , do i need to call createMessageOnQueue
method second time. If yes, will it not create new queue with
session.createQueue("TEST.FOO")?
session.createQueue("TEST.FOO") does not necessarily create queue, it just get a reference to existing queue.
javadoc of session#createQueue()
Note that this method simply creates an object that encapsulates the
name of a topic. It does not create the physical topic in the JMS
provider. JMS does not provide a method to create the physical topic,
since this would be specific to a given JMS provider. Creating a
physical topic is provider-specific and is typically an administrative
task performed by an administrator, though some providers may create
them automatically when needed.
The queue is created once and only you can delete it manually.
Once the message is sent to a queue, it will wait on the queue until it's consumed (unlike topics).
You don't need to re-create the message if you want to send it twice. But then again, why would you send it two times?
I feel that your problem might be solved using JMS transactions.