On Mon, 29 Jun 2020 at 13:18, akabhishek1
<[email protected]> wrote:
>
> Hey Tim,
>
> Thanks a lot for quick reply. I tested with your suggestion, and I am able
> to set custom message ID with the help of CustomJmsMessageIDPolicy,
> But we have to apply this policy at the connection level, which is a big
> problem for us because we can't have a new connection for every new message.
>
To be clear, the policy is set per connection but it is used over time
to create per-producer ID builders which are then called per-send to
create the IDs.
> Every message will have different custom MessgeID and we need to set/send
> application defined messageID.
>
> Can you please suggest anyway to give precedence of setting messageID on
> JMSMessage?
> I tried to use "textMessage.setJMSMessageID("custom-ID");", but this doesn't
> help.
Right, as that's not what the method is for, and it is also not for
application usage at all. As Tim already outlined along with relevant
quotes from the JMS spec, the JMS provider sets the JMSMessageID value
during send().
>
> Please suggest any approach to define custom messageID with the help of
> JmsMessage object.
>
There isn't a route to achieve that using the message object since as
before the JMSMessageID is set by the client upon send() being called.
The message ID value that is set being the [changing] value that is
returned from the message ID builder upon send() on a producer being
called (or null if the producers disableMessageID hint has been set).
The ID builder having been created for that producer from the ID
policy object when the producer was created.
If you want to control the ID value used upon the next send() call you
could perhaps have your custom ID builders utilise a ThreadLocal value
in the creation logic, that you can then set beforehand toward
populating the value 'created' and returned by the ID builder when
next used during a send() call on that thread.
> Example -
>
> import java.util.Hashtable;
> import javax.jms.Connection;
> import javax.jms.Destination;
> import javax.jms.ExceptionListener;
> import javax.jms.JMSException;
> import javax.jms.MessageProducer;
> import javax.jms.Session;
> import javax.jms.TextMessage;
> import javax.naming.Context;
> import javax.naming.InitialContext;
> import javax.naming.NamingException;
> import org.apache.qpid.jms.JmsConnectionFactory;
> import org.apache.qpid.jms.JmsDestination;
> import org.apache.qpid.jms.JmsSession;
> import org.apache.qpid.jms.message.JmsMessage;
> import org.apache.qpid.jms.message.JmsMessageIDBuilder;
> import org.apache.qpid.jms.policy.JmsMessageIDPolicy;
> import org.apache.qpid.jms.provider.amqp.message.AmqpJmsMessageFacade;
>
> public class TestQpidSendCustomMessageID implements ExceptionListener{
>
> private static final String QUEUE_NAME = "test-msg-queue";
> private static final String SBUS_NAME = "#####";
> private static final String USERNAME = "######";
> private static final String PASSWORD = "####";
> private static final String QPID_CONNECTION_FACTORY_CLASS =
> "org.apache.qpid.jms.jndi.JmsInitialContextFactory";
>
> public static void main(String[] args) throws Exception {
> TestQpidSendCustomMessageID test = new
> TestQpidSendCustomMessageID();
> test.send();
> }
>
> private void send() throws NamingException, JMSException,
> InterruptedException {
> Hashtable<String, String> hashtable = new Hashtable<>();
>
> hashtable.put("connectionfactory.SBCF", "amqps://"+ SBUS_NAME
> +".servicebus.windows.net?transport.tcpKeepAlive=true&amqp.traceFrames=true&jms.prefetchPolicy.all=1000&jms.forceAsyncSend=true");
>
> hashtable.put(Context.INITIAL_CONTEXT_FACTORY,
> QPID_CONNECTION_FACTORY_CLASS);
>
> Context context = new InitialContext(hashtable);
>
> JmsConnectionFactory connectionFactory =
> (JmsConnectionFactory)
> context.lookup("SBCF");
>
> //Set Custom Jms MessageID Policy
> CustomJmsMessageIDPolicy customJmsMessageIDPolicy = new
> CustomJmsMessageIDPolicy();
>
> connectionFactory.setMessageIDPolicy(customJmsMessageIDPolicy);
>
> Connection connection =
> connectionFactory.createConnection(USERNAME,
> PASSWORD);
> connection.setExceptionListener(this);
>
> connection.start();
>
> Session session = connection.createSession(false,
> Session.CLIENT_ACKNOWLEDGE);
> Destination destination = session.createQueue(QUEUE_NAME);
> MessageProducer messageProducer =
> session.createProducer(destination);
>
> TextMessage textMessage = session.createTextMessage("Hello
> -QA ASD");
> textMessage.setJMSMessageID("Hello-SetJMSID"); //First Way
>
> ((AmqpJmsMessageFacade) ((JmsMessage)
> textMessage).getFacade()).setMessageId("AKQWS:9d678sdfsdsds"); //Second Way
>
> messageProducer.send(textMessage);
>
> System.out.println("**** Published successfully ****");
>
> }
>
> @Override
> public void onException(JMSException exception) {
> exception.printStackTrace();
> }
>
> class CustomJmsMessageIDPolicy implements JmsMessageIDPolicy{
>
> @Override
> public JmsMessageIDPolicy copy() {
> return new CustomJmsMessageIDPolicy();
> }
>
> @Override
> public JmsMessageIDBuilder getMessageIDBuilder(JmsSession
> session,
> JmsDestination destination) {
>
> return new JmsMessageIDBuilder() {
>
> @Override
> public Object createMessageID(String
> producerId, long messageSequence) {
> return "test-custom-msg-id"; // How
> can we set message from JmsMessage
> Object?
> }
> };
>
> }
>
> }
>
>
> }
>
>
>
>
> --
> Sent from: http://qpid.2158936.n2.nabble.com/Apache-Qpid-users-f2158936.html
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]