how to pass XQServiceContext as reference in Java Custom Ser

Posted by Admin on 14-Jul-2008 19:48

Hi All,

I have created java custom service and passed XQServiceContext and incoming message(XQEnvelop) into the another method (seperate java class) within the service method. Then I'm doing some processing things getting message contents and reset the message again to XQEnvelope. Next , I have attached it the context ,but messages are not going to the destination.

Waiting for help

Thanks,

Sampath

please refer my sample code

public void service(XQServiceContext ctx) throws XQServiceException {

XQEnvelope env = null;

while (ctx.hasNextIncoming()) {

env = ctx.getNextIncoming();

if (env != null) {

XQMessage msg = env.getMessage();

try {

MessageProcessor processor = MessageProcessor.getInstance();

processor.doSomething(ctx,env);

} catch (XQMessageException me) {

throw new XQServiceException("Exception accessing XQMessage: " + me.getMessage(), me);

} catch (JMSException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

/* Iterator addressList = env.getAddresses();

if (addressList.hasNext()) {

ctx.addOutgoing(env);

}

*/

}

}

}

Method

public void doSomething(XQServiceContext ctx,XQEnvelope env)throws XQServiceException {

XQMessage msg = env.getMessage();

//do something

env.setMessage(msg);

Iterator addressList = env.getAddresses();

if (addressList.hasNext()) {

com.sonicsw.xq.XQAddress address = (com.sonicsw.xq.XQAddress) addressList.next();

system.info( "Target Destination : " + address.getName());//This is printing my destination properly.

ctx.addOutgoing(env);

}

}

All Replies

Posted by jtownsen on 15-Jul-2008 08:45

public void service(XQServiceContext ctx) throws XQServiceException {

m_xqLog.logDebug(m_logPrefix + "Service processing...");

XQEnvelope env = null;

while (ctx.hasNextIncoming()) {

env = ctx.getNextIncoming();

if (env != null) {

XQMessage msg = env.getMessage();

MessageProcessor processor = MessageProcessor.getInstance();

processor.doSomething(ctx,env);

}

m_xqLog.logDebug(m_logPrefix + "Service processed...");

}

}

import com.sonicsw.xq.*;

import java.util.*;

public class MessageProcessor {

public static MessageProcessor getInstance() {

MessageProcessor mp = new MessageProcessor();

return mp;

}

public void doSomething(XQServiceContext ctx,XQEnvelope env)throws XQServiceException {

XQMessage msg = env.getMessage();

// do something

env.setMessage(msg);

Iterator addressList = env.getAddresses();

if (addressList.hasNext()) {

com.sonicsw.xq.XQAddress address = (com.sonicsw.xq.XQAddress) addressList.next();

System.out.println( "Target Destination : " + address.getName());//This is printing my destination properly.

ctx.addOutgoing(env);

}

}

}

Posted by Admin on 21-Jul-2008 20:21

Hi,

I’m using Sonic 7.6, my problem is I’m trying to pass XQServiceContext into my own thread and do the rest.

My Scenario is,

When message received into java custom service, I’m putting that message into message container (Vector) and notifying to my worker threads. In my worker thread class , doing all the processing things and next trying to send that message into the destination using XQServiceContext (I have passed XQServiceContext as refrence into thread). But it never reached to the destination.

Pls help me to solve this.

Thanks,

Anura

Posted by jet on 29-Jul-2008 15:18

Anura,

As I'm not familiar with your implementation of the XQService, other than what I've read, it sounds like you're passing off a copy of the service context and message to a worker thread, that thread likely one from a pool?

If so, why not use the XQDispatcher interface to asynchronously drop the message back into ESB so it can get to it's destination? I haven't tested this with our threaded service, but am guessing it would work. I have, however, used the XQDispatcher interface in another custom service and it works beautifully.

We use a solution that sounds similar to yours. However, our worker thread returns the message to the XQService class, after it's done manipulating the message, where its added to the outgoing box.

The XQService, thread pool, and thread worker class are all thread-safe and work great, with multiple instances of the service deployed to the ESB container (multiple instances as in # of threads (listeners) selected for the service-instance that's deployed to the ESB container.

Hope this helps...

-Jet.

Posted by Admin on 29-Jul-2008 21:41

Hi Jet,

Thanks for good point, but according to your scenario, you have initially defined XQService thread pool. Then how we can dynamically increase no of threads in the pool according to message capacity (I hope you have setup the thread pool in ESB container and not in programming code). Also if you can provide simple sample to implement dispatcher that will be great help for me.

Thanks

Anura

Posted by jet on 04-Aug-2008 10:58

Anura,

You'd need to implement a method to check the available objects in the pool prior to checking an object out, and if none exist, then create another object in the pool to use before returning the thread to the pool. Otherwise, you would have to have the thread wait until an object is next available for checkout.

Here is a simple example of implementing the XQDispatcher:

m_serviceCtx.getDispatcher().

dispatch(m_serviceCtx.getEnvelopeFactory().

createTargetedEnvelope(dispatchAddress, workMsg),

XQQualityofService.AT_LEAST_ONCE);

where:

1. dispatchAddress = ESB address (XQAddress) to another ESB endpoint (service,process,queue,etc).

2. workMsg = XQMessage

If I recall correct I believe the ESB sample exercises had something that might be a little more thorough, but this would show you how to use the interface to dispatch a message back into ESB.

This thread is closed