Parsing Non-Blocking Data

I'm presently using non blocking sockets to receive data from a server. The problem is that I receive several chunks of data all at once. Is there a way to parse the data using a terminating byte instead of the usual -1 EOF?

I receive data continuously so given: channel.read(buffer) I just run out of bufferspace before I am given the chance to parse it. Bottomline I want to replace this blocking code:
DataInputStream dis = new DataInputStream(socket.getInputStream());
while((currentByte = dis.read()) != ETX) //ETX is the terminating byte
     buffer.put((byte)currentByte);
with its nonblocking counterpart.

Similar Messages

  • How to insert multiple data to non database data block?

    Hi all,
    I'm new in using form builder. I have a question about insert multiple data to non database data block.
    I want to upload a csv file that content the attendance transaction of all of employees. If the employee in that
    file doesn't exist in that company,so I must record the employee number into non database data block.
    If I just upload one employee data, I can record it to that data block. But, if more than one, it didn't works.
    May you help me?
    Thanks..
    Tika

    you might need a NEXT_RECORD or CREATE_RECORD to go to the next reord in that block. It would be easier to help if we knew your code.

  • Non-Blocking I/O Implementation Issue

    Hi All,
    I am trying out the latest JDK 1.4 java.nio.* package. I modified the NBTimeServer and wrote a client which connects to the NBTimeServer and tries to pass messages to and fro. I always succeed to pass on roundtrip of msgs and then Server blocks my client forever. I have modified NBTimeServer to accomodate one client only. Any help or comments on this would be really appreciated. Code is below. Feel free to try it out if want to see what I am trying to convey in this message.
    /*******Server Code*******/
    Modified this code to test mulitple to and fro msgs between client and server.
    Only one client will ever be able to connect to this server during life of a server.
    My point here is to demonstrate the to and fro comm between one client and one server
    import java.io.*;
    import java.nio.*;
    import java.nio.channels.*;
    import java.nio.channels.spi.*;
    import java.net.*;
    import java.util.*;
    import java.nio.charset.*;
    import java.util.regex.*;
    // Listen on a port for connections and write back the current time.
    public class NBTimeServer {
    private static final int DEFAULT_TIME_PORT = 8900;
    // Constructor with no arguments creates a time server on default port.
    public NBTimeServer() throws Exception {
         acceptConnections(this.DEFAULT_TIME_PORT);
    // Constructor with port argument creates a time server on specified port.
    public NBTimeServer(int port) throws Exception {
         acceptConnections(port);
    // Accept connections for current time. Lazy Exception thrown.
    private static void acceptConnections(int port) throws Exception {
         // Selector for incoming time requests
         Selector acceptSelector = SelectorProvider.provider().openSelector();
         Selector rwSelector = SelectorProvider.provider().openSelector();
         // Create a new server socket and set to non blocking mode
         ServerSocketChannel ssc = ServerSocketChannel.open();
         ssc.configureBlocking(false);
         // Bind the server socket to the local host and port
         InetAddress lh = InetAddress.getLocalHost();
         InetSocketAddress isa = new InetSocketAddress(lh, port);
         ssc.socket().bind(isa);
         // Register accepts on the server socket with the selector. This
         // step tells the selector that the socket wants to be put on the
         // ready list when accept operations occur, so allowing multiplexed
         // non-blocking I/O to take place.
         SelectionKey acceptKey = ssc.register(acceptSelector,
                             SelectionKey.OP_ACCEPT);
         int keysAdded = 0;
         // Here's where everything happens. The select method will
         // return when any operations registered above have occurred, the
         // thread has been interrupted, etc.
         while ((keysAdded = acceptSelector.select()) > 0) {
         // Someone is ready for I/O, get the ready keys
         Set readyKeys = acceptSelector.selectedKeys();
         Iterator i = readyKeys.iterator();
         // Walk through the ready keys collection and process date requests.
         while (i.hasNext()) {
              SelectionKey sk = (SelectionKey)i.next();
              i.remove();
              // The key indexes into the selector so you
              // can retrieve the socket that's ready for I/O
              ServerSocketChannel nextReady =
              (ServerSocketChannel)sk.channel();
              // Accept the date request and send back the date string
              Socket s = nextReady.accept();
                        SocketChannel sc = s.getChannel();
    System.out.println("Got client channel..");
              sc.configureBlocking(false);
              SelectionKey readKey = sc.register(rwSelector,
                             SelectionKey.OP_READ|SelectionKey.OP_WRITE);                     
              int count = 0;
    while(true) {
    if((count = rwSelector.select(1000L)) > 0) {
    Set readKeys = rwSelector.selectedKeys();
    Iterator i1 = readKeys.iterator();
    while(i1.hasNext()) {
    System.out.println("Loop in Iterator");
    SelectionKey sk1 = (SelectionKey)i1.next();
    i1.remove();
    if(sk1.isReadable()) {
    DataInputStream sin = new DataInputStream(new BufferedInputStream(s.getInputStream(), 4096));
    System.out.println(sin.readInt());
    if(sk1.isWritable()) {
    DataOutputStream sout = new DataOutputStream(new BufferedOutputStream(s.getOutputStream(), 4096));
    PrintWriter out = new PrintWriter(sout, true);
              Date now = new Date();
              out.println(now);
    // Entry point.
    public static void main(String[] args) {
         // Parse command line arguments and
         // create a new time server (no arguments yet)
         try {
              NBTimeServer nbt = new NBTimeServer();
         } catch(Exception e) {
              e.printStackTrace();          
    /******End Server Code********/
    /*****Begin Client Code********/
    import java.io.*;
    import java.net.*;
    import java.util.*;
    // Listen on a port for connections and write back the current time.
    public class NBTimeClient {
    private static final int DEFAULT_TIME_PORT = 8900;
    public static void main(String args[]) throws Exception {
    InetAddress lh = InetAddress.getLocalHost();
    Socket s = new Socket(lh, DEFAULT_TIME_PORT);
    DataInputStream din = new DataInputStream(new BufferedInputStream(s.getInputStream(), 4096));
    DataOutputStream dout = new DataOutputStream(new BufferedOutputStream(s.getOutputStream(), 4096));
    //Read the time
    System.out.println(din.readLine());
    //send some junk which is read by server
    dout.writeInt(1299);
    dout.flush();
    //read time again -- I never get anything here and I am blocked here...
    System.out.println(din.readLine());
    //send some junk back to the server
    dout.writeInt(1299);
    dout.flush();
    s.close();
    /*******End Client Code**********/
    thanks,
    Xtrimity

    The reason it blocks forever is that you need to keep reusing your main select. That is where the non-blocking event will come from. Here is a bit of code that doesn't block forever.
    Tim
    http://tim.owlmountain.com
    import java.io.*;
    import java.nio.*;
    import java.nio.channels.*;
    import java.nio.channels.spi.*;
    import java.net.*;
    import java.util.*;
    import org.apache.log4j.*;
    public class NBServer3 {
    int _port = 4000;
    Selector _selector = null;
    ServerSocketChannel _selectableChannel = null;
    int _keysAdded = 0;
    static Category log =
    Category.getInstance(NBServer3.class.getName());
    static String QUIT_SERVER = "quit";
    public NBServer3() {
    public NBServer3( int port ) {
    this._port = port;
    public void initialize()
    throws IOException {
    this._selector = SelectorProvider.provider().openSelector();
    this._selectableChannel = ServerSocketChannel.open();
         this._selectableChannel.configureBlocking(false);
         InetAddress lh = InetAddress.getLocalHost();
         InetSocketAddress isa = new InetSocketAddress(lh, this._port );
         this._selectableChannel.socket().bind(isa);
    public void finalize()
    throws IOException {
    this._selectableChannel.close();
    this._selector.close();
    public void acceptConnections()
    throws IOException {
    Selects a set of keys whose corresponding channels are ready for I/O
    operations. This method performs a non-blocking selection operation.
    If no channels have become selectable since the previous selection
    operation then this method immediately returns zero.
    Returns:
    The number of keys, possibly zero, whose ready-operation sets
    were updated by the selection operation
    do {
    SelectionKey acceptKey =
    this._selectableChannel.register( this._selector,
    SelectionKey.OP_ACCEPT );
    log.debug( "Acceptor loop..." );
    while (( this._keysAdded = acceptKey.selector().select()) > 0 ) {
    log.debug( "Selector returned "
    + this._keysAdded + " ready for IO operations" );
    Set readyKeys = this._selector.selectedKeys();
    Iterator i = readyKeys.iterator();
    while (i.hasNext()) {
    SelectionKey key = (SelectionKey)i.next();
    i.remove();
    if ( key.isAcceptable() ) {
    ServerSocketChannel nextReady =
    (ServerSocketChannel)key.channel();
    log.debug( "Processing selection key read="
    + key.isReadable() + " write=" + key.isWritable() +
    " accept=" + key.isAcceptable() );
    Socket s = nextReady.accept();
    s.getChannel().configureBlocking( false );
    SelectionKey readKey =
    s.getChannel().register( this._selector,
    SelectionKey.OP_READ );
    readKey.attach( s );
    else if ( key.isReadable() ) {
    SelectableChannel nextReady =
    (SelectableChannel) key.channel();
    log.debug( "Processing selection key read="
    + key.isReadable() + " write=" + key.isWritable() +
    " accept=" + key.isAcceptable() );
    Socket socket = (Socket) key.attachment();
    BufferedReader in = new BufferedReader(
    new InputStreamReader( socket.getInputStream() ));
    String line = null;
    if ( (line = in.readLine() ) != null )
    log.debug( line );
    log.debug( "End acceptor loop..." );
    } while ( false ); //FIXIT tim this should be false. justa test
    public static void main( String[] args ) {
    BasicConfigurator.configure();
    NBServer3 nbServer = new NBServer3();
    try {
    nbServer.initialize();
    } catch ( IOException e ) {
    e.printStackTrace();
    System.exit( -1 );
    try {
    nbServer.acceptConnections();
    catch ( IOException e ) {
    e.printStackTrace();
    log.error( e );

  • What is wrong with my non-blocking client?

    I have two classes here, my abstract base class SingleSocketHandler, and its concrete subclass SingleSocketHandlerImpl. The subclass implements the protocol and parsing of my server.
    For some reason, my server is not receiving the packet my client sends to it, and my client is getitng nothing in return (which makes sense, the server is supposed to respond to the logon packet). I make it non-blocking AFTER logon, so I knwo that this is not a problem. Can you see why my server is not receiving the packet my client writes to it? Did I not configure some setting with the SocketChannel that enables it to write? I am sort of unfamiliar with the java.nio.channels package, so the problem may be related to a setting in the SocketChannel or whatnot that I haven't configured.
    NOTE: My chat server works fine with my blocking, multi-threaded test clients. Just not for my non-blocking client. The original problem for my blocking clients was that once the server stopped sending them data, they'd get caught in the in.read() loop and never get out of it. That's why I turned to non-blocking.
    Just to remind you, my question is: why isn't my client sending the logon packet AND/OR my server receiving+responding to it?
    public abstract class SingleSocketHandler extends Thread
         /* Subclasses must implement these methods
            /* Even though they're not a (public) interface */
         /** <------------------------------- */
              abstract void parse(int num);
              abstract void parseNext();
              abstract void doLogon();
         /** -------------------------------> */
         private SocketChannel sock;
         /* Queues for in and out buffers */
         private LinkedList <ByteBuffer> qIn;
         private LinkedList <ByteBuffer> qOut;
         /* Server info */
         private String hostname;
         private int port;
         /* Flags */
         protected int flags;
              protected final int LOGGED_ON = 0x01;
          * Default Constructor
         protected SingleSocketHandler()
              initQs();
          * Constructor that sets socket info
          * @param hostname
          * @param port
          * @param connect
         protected SingleSocketHandler(String hostname, int port, boolean connect)
              initQs();
              if (connect)
                   connect(hostname, port);
              else
                   setSocket(hostname, port);
          * Switches off between reading and writing
         protected void handleIO()
              try
                   sock.configureBlocking(false);
              } catch (IOException e)
                   // TODO
              readInBuffers(1);
              writeOutBuffers(1);
          * Read in specified number of buffers into in queue
          * Call for parsing
          * @param num
         protected void readInBuffers(int num)
              Reporter.println("READING BUFFER");
              for (int i = 0; i < num; i++)
                   ByteBuffer header = ByteBuffer.allocate(ProtocolCheck.HEADER_LEN);
                   try
                        Reporter.println("Reading header...");
                        sock.read(header);
                        Reporter.println("Read header.");
                   } catch (IOException e)
                        // TODO
                   /* Only add packet to in queue if it has a valid header */
                   if (ProtocolCheck.validHeader(header.array()))
                        Reporter.println("valid header");
                        ByteBuffer packet = ByteBuffer.allocate(ProtocolCheck.findPacketLen(header.array()));
                        packet.put(header);
                        try
                             Reporter.println("Reading rest of packet...");
                             sock.read(packet);
                             Reporter.println("Read packet.");
                        } catch (IOException e)
                             // TODO
                        addInBuffer(packet);
          * Write out specified number of buffers from out queue
          * And remove from out queue
          * @param num
         protected void writeOutBuffers(int num)
              Reporter.println("WRITING BUFFER");
              int i = 0;
              while (qOut.size() > 0 && i < num)
                   try
                        sock.write(nextOutBuffer());
                        Reporter.println("Wrote buffer.");
                   } catch (IOException e)
                        // TODO
                   i++;
          * Returns and removes next buffer from in queue
          * @return ByteBuffer
         protected ByteBuffer nextInBuffer()
              return qIn.remove();
          * Returns and removes next buffer from out queue
          * @return ByteBuffer
         protected ByteBuffer nextOutBuffer()
              return qOut.remove();
          * Sees if there is anohter in buffer
          * @return boolean
         protected boolean hasNextInBuffer()
              return qIn.size() > 0;
          * Sees if there is another out buffer
          * @return ByteBuffer
         protected boolean hasNextOutBuffer()
              return qOut.size() > 0;
          * Add a buffer to in queue
          * @param b
         public void addInBuffer(ByteBuffer b)
              qIn.add(b);
          * Add a buffer to in queue
          * @param b
         public void addInBuffer(Bufferable b)
              qIn.add(b.getByteBuffer());
          * Add a buffer to out queue
          * @param b
         public void addOutBuffer(ByteBuffer b)
              qOut.add(b);
          * Add a buffer to out queue
          * @param b
         public void addOutBuffer(Bufferable b)
              qOut.add(b.getByteBuffer());
          * Instantiate queues
         protected void initQs()
              qIn = new LinkedList <ByteBuffer> ();
              qOut = new LinkedList <ByteBuffer> ();
          * Set socket info then call connect()
          * @param hostname
          * @param port
         public void connect(String hostname, int port)
              setSocket(hostname, port);
              connect();
          * Connect to server
         public void connect()
              try
                   sock = SocketChannel.open();
                   sock.configureBlocking(true);
                   sock.connect(new InetSocketAddress(hostname, port));
                   while (!sock.finishConnect())
              } catch (IOException e)
                   // TODO
          * Disconnect from server
         public void disconnect()
              try
                   sock.close();
              } catch (IOException e)
                   // TODO
          * Set socket info without connecting
          * @param hostname
          * @param port
         public void setSocket(String hostname, int port)
              this.hostname = hostname;
              this.port = port;
          * @return state of connection
         public boolean isConnected()
              return (sock != null && sock.isConnected());
          * @return state of being logged on
         public boolean isLoggedOn()
              return (sock != null && (flags & LOGGED_ON) == LOGGED_ON);
    public final class SingleSocketHandlerImpl extends SingleSocketHandler
         private UserDatabase <User> users;
          * Constructor that does not set socket info
         public SingleSocketHandlerImpl(UserDatabase <User> users)
              super();
              this.users = users;
          * Constructor that does set socket info
          * @param hostname
          * @param port
          * @param connect
         public SingleSocketHandlerImpl(String hostname, int port, boolean connect, UserDatabase <User> users)
              super(hostname, port, connect);
              this.users = users;
          * Thread's run method (base class extends Thread)
         public void run()
              doLogon();
              while (isConnected() && isLoggedOn())
                   handleIO();
          * Parses specified number of buffers from in queue
          * @param num
         /* (non-Javadoc)
          * @see client.SingleSocketHandler#parseNext()
         @Override
         protected void parse(int num)
              Reporter.println("Parse(int num) called.");
              int i = 0;
              while (hasNextInBuffer() && i < num)
                   parseNext();
                   i++;
         /* (non-Javadoc)
          * @see client.SingleSocketHandler#parseNext()
         @Override
         protected void parseNext()
              Reporter.println("Parsing!");
              if (!hasNextInBuffer())
                   Reporter.println("NO IN BUFFER.");
                   return;
              /* Get buffer to work with */
              ByteBuffer inBuffer = nextInBuffer();
              byte[] data = inBuffer.array();
              /* Decide what to do based on message ID */
              byte msgid = data[1];
              switch (msgid) {
              case 0x01:
                   Reporter.println("0x01 packet.");
                   /* Determine success of login */
                   byte success = data[3];
                   if (success == (byte) 1)
                        flags |= LOGGED_ON;
                        Reporter.println("Logged on!");
                   else
                        flags &= ~LOGGED_ON;
                        Reporter.println(" <eChat> Unable to logon. Check the hostname and port settings.");
                   break;
              case 0x02:
                   /* Parse out text message */
                   byte[] txtmsgbytes = new byte[data.length - 3];
                   System.arraycopy(data, 3, txtmsgbytes,  0, txtmsgbytes.length);
                   String txtmsg = new String(txtmsgbytes);
                   Reporter.println(txtmsg);
                   break;
              case 0x03:
                   System.out.println("Packet ID not yet handled.");
                   break;
              case 0x04:
                   System.out.println("Packet ID not yet handled.");
                   break;
              default:
                   System.out.println("validID() method is buggy.");
             * I make it non-blocking after logon sequences
         /* (non-Javadoc)
          * @see client.SingleSocketHandler#doLogon()
         @Override
         protected void doLogon()
              Reporter.println("DOING LOGON!");
              User myUser = users.getCurr();
              addOutBuffer(new ScpLogon(myUser.getUsername(), myUser.getPassword()));
              writeOutBuffers(1);
              readInBuffers(1);
              parseNext();
    }

    Oh, if this helps, this is what gets output to my GUI. I did a lot of outputs for debugging purposes.
    [3:29:27 PM]: Connecting...
    [3:29:27 PM]: Connected!
    [3:29:27 PM]: Logging on...
    [3:29:27 PM]: DOING LOGON!
    [3:29:27 PM]: WRITING BUFFER
    [3:29:27 PM]: Wrote buffer.
    [3:29:27 PM]: READING BUFFER
    [3:29:27 PM]: Reading header...

  • Non-blocking file access?

    1. Did I miss something: is there a way in java 1.5 (or even java 1.6) to read files and query file metadata (size, last modified, that kind of thing) in a non-blocking manner?
    2. Is there a way to figure out where physical harddrive boundaries lie without resorting to tricks like running 'df' and parsing the output, which is rather un-portable? I obviously don't want to create a thread per file read (one aim is that the maximum number of threads is constant - even with a gazillion requests the amount of threads will never exceed a (low) constant.) but just 1 thread to perform all required reads would possibly end up wasting time if there are multiple harddrives and the CPU is idling anyway. I'd guess at this point that 1 thread per physical harddrive handling all file access is the best way to go, but how do I figure out those boundaries?
    3. Is NIO2 (JSR 203) going to add features to work with files in a non-blocking way?
    4. Is there a full-stack (templating, database access, writing web services, serving up the data) web server out there someplace that works entirely on a non-blocking methodology? I don't think so, but I might have missed something. JETTY tries something, but the servlet specs fundamentally do not work with non-blocking I/O, so it doesn't count.
    background:
    I'm writing a non-blocking webserver. KISS is the attitude - so far the basic system and any services (my take on the servlet spec, as the servlet spec doesn't play nice with NIO principles) on it all run inside a single thread, no thread pool.
    So far it all works, and stress testing it with 1000 connections, 200 at the same time shows a process time of about half a millisecond per request, on a simple macbook, which is nice, because that beats apache even at serving up 404s.
    So far I have only written very simple test apps that for example do calculations and the like, nothing that you'd actually do with web apps, like, say, serving (template-powered) data from files on a harddrive.
    Because of the absolute requirement for each service NEVER to block while serving up bytes, manually working with files would be an unbelievable pain on the part of the web service developer. Hence I'm building templating and DB access into the system.
    Unfortunately, I realized, reading NIO specs, that there doesn't seem to be any way to perform file operations in a non-blocking manner (not just reading them, but getting file size and last modified timestamps as well). I'll obviously have to use a lot of caching tactics, but before I can cache, I need to read files somehow.
    The aim of this webserver is basically to survive massive spikes in traffic, be it from digg/slashdot, or from a DDOS attack, as best as possible, and where that isn't possible, to gracefully fall apart. That is, service as many requests as possible at a certain minimum bandwidth per request, prioritizing repeat visitors and site moderators, and deny the rest instantly, instead of just falling apart in shambles as happends in your usual LAMP or servlet design, not serving even 1 request right most of the time.
    Thanks a lot for everyone's time!

    ejp wrote:
    BufferedInputStream will improve the performance by a factor of several thousand because it cuts down on system calls.Well, that might be true if you're reading individual bytes with a call to read(), or making many small read( buffer ) requests. But if you call read( buffer ) with a reasonably sized buffer (BufferedInputStream uses 8192 bytes as a default buffer size), then you're going to get performance equal to or possibly better than a BufferedInputStream.

  • Implementing non-blocking read

    Hi all
    I have some doubts about implementing non-blocking io in my application.
    I am using Jsch library (an ssh implementation) to open an ssh connection with a host. The API only provides me with methods to open a connection and retreive the input & output streams. I want to make my read on inputstream non-blocking.In such a case is it possible to use nio for the purpose?
    If it's not possible then I am planning to use threading to make read() non-blocking. Here also i need some clarifications. I am planning to use a ThreadPoolExecutor to create a thread pool for reading data. SO whenever i have a read i'll assign this task to the pool which will use one of the free threads to execute the inputStresm.read().
    Now the question is if one of the threads in this pool blocks forever during a read since it didn't get any response from the other side, is there a way to stop that read and make that thread free again to execute more tasks? or will the thread block forever till the application is closed?
    In my case i cannot afford to have too many such blocked threads, since this application will not be restarted very often. Once it is started it can go on for may be days or months.
    Please suggest what would be best in my case taking into account performance as most important factor.
    Thanks in advance.
    Anu

    endasil wrote:
    First of all, let me state that I agree with the others in saying that I don't fully agree with your premises.
    That said, I believe that this does a non-blocking read based on the contract of InputStream.available() and .read(byte[], int, int):
    private int nonBlockingRead(InputStream in, byte[] buffer) throws IOException {
    return in.read(buffer, 0, Math.min(in.available(), buffer.length));
    If the InputStream is obtained from a JSSE socket then it is my understanding that available() always returns zero. This is allowed under the InputStream.available() contract as defined in the Javadoc - http://java.sun.com/javase/6/docs/api/java/io/InputStream.html#available() . If I am right then your code will never read anything from a JSSE socket InputStream and I would suspect that Jsch is using JSSE sockets.

  • NIO: Strange problem when using ByteBuffer with non-blocking SocketChannel

    Hi,
    I have a server that uses multiplexed, non-blocking I/O with java.nio. When a client connects, the server waits for the message: <system cmd="knock"/>, returns a message and disconnects the client. The clients are shortly serviced in less than a second.
    But the server newer receive anything from about 20% of the clients - even though it is sent. Or with other words: it is received and the data is contained in the ByteBuffer - SocketChannel.read(ByteBuffer) - but a call to ByteBuffer.remaing() returns 0 !!
    ByteBuffer receiveBuf = ByteBuffer.allocate(65536);
    receiveBuf.clear(); // the code is elsewhere used for longer living clients
    int readBytes = channel.read(receiveBuf);
    receiveBuf.flip();
    StringBuffer sb = new StringBuffer();
    System.out.println(" * Remaining: "+receiveBuf.remaining()); // writes: ' * Remaining: 0'
    System.out.println(" * Received: "+new String(receiveBuf.array())); // writes: ' * Received: <system cmd="knock"/>'
    while(receiveBuf.remaining() >= 2) {
      byte b = receiveBuf.get();
      sb.append((char)b);
    System.out.println(" * sb content: "+sb.toString()); // writes: ' * sb content: 'The ByteBuffer clearly receives the correct data, but the ByteBuffer.remaining() returns 0 and therefore the StringBuffer will never have any content.
    The problem seems to occur randomly and for about 20% of the clients (simulated from the same computer and therefore has the same bandwidth and so on).
    Anyone knows what is going on, and how to solve the problem !?

    It's always possible in any read that the number of bytes read is less than the number of bytes requested. You need to keep reading until you have got everything you expected, and cope with every possible error condition on each iteration.
    EJP

  • Non blocking worker servlet ?

    Hi friends,
    I have an issue to implement. We implemented a web application and now I want to separate some parts of this application. I can express the action shorter and simple below.
    if a user requests x-y ticket {
    ���try {
    ������check db if x-y is available
    ������if true send available
    ������else not
    ���}catch (NonExistentData) {
    ������addJob2Queue(x-y)
    ������send available
    ���}
    class AvailabilityHelper extends Thread{
    ���run(){
    ������while(true){
    ���������checkQueue
    ���������if there is job, updateDBfor(org,dest)
    ������}
    ���}
    * updateDBfor(org,dest) includes other ejb references to request data and insert it into db
    This web aplication runs with JSP 1.2 EL support on WAS 5.1
    the check for db and the thread implementation was in a seperate package and were used in actions of web application. Now they also want to use these checks for other (non web) applications. So i think there should be a session bean in ejb and should process these requests and I used JMS (Message-Driven Bean) with WebSphere MQ to undertake the thread and queue mechanism.
    Now it works like this :
    class exampleSessionBean{
    ���getAvalibability(org, dest){
    ������try {
    ���������check db if x-y is available
    ���������if true send available
    ���������else not
    ������}catch (NonExistentData) {
    ���������queueSender.sendMessage(update x-y)
    ���������send available;
    ������}
    ���}
    class queueMDB{
    ���onMessage(){
    ������updateDBfor(org,dest)
    ���}
    The purpose of this applications was, not to block the user for db update process, if there is data in db select and show to the user. the update process of db includes other ejb references and takes so much time. It is nonsense to make the user wait such time. And the info is not a big deal, because of this when i put a request for update, without blocking i send it as available...
    So what is the problem ? :) Deployment team don't want to pay for websphere MQ and hire a admin for that. Also they don't want to use Embedded Messaging server client (JMS Server) of WAS. So I am asking you: is there a way to implement a non blocking servlet so that i will just make a request and don't wait for response, or a servlet will take the request and release the connection but will continue to process the request ?
    thanks for all...

    I tried to implement a web application and add a ServletContextListener as stevejluke suggests. But on contextInitialized function when creating new JobConsumer it gets following errors on server.
    6/27/06 12:51:36:391 CEST] 33238138 WebContainer A SRVE0169I: Loading Web Module: non-blocking.
    [6/27/06 12:51:36:438 CEST] 33238138 SystemOut O OlaListener initializing...
    [6/27/06 12:51:36:516 CEST] 33238138 SystemOut O OlaListener - Queue added
    [6/27/06 12:51:36:516 CEST] 33238138 SystemOut O OlaListener - keepWorking
    [6/27/06 12:51:36:672 CEST] 33238138 WebApp E SRVE0015E: Failure to initialize Web application non-blocking
    [6/27/06 12:51:36:844 CEST] 33238138 WebGroup E SRVE0054E: An error occurred while loading Web application
    public class OlaListener implements ServletContextListener {
         * @see javax.servlet.ServletContextListener#void (javax.servlet.ServletContextEvent)
         public void contextDestroyed(ServletContextEvent event) {
                 ServletContext context = event.getServletContext();
                 context.setAttribute("keepWorking", Boolean.FALSE);
         * @see javax.servlet.ServletContextListener#void (javax.servlet.ServletContextEvent)
         public void contextInitialized(ServletContextEvent event) {
                 System.out.println("OlaListener initializing...");
                 ServletContext context = event.getServletContext();
                 WorkQueue newJobs = new WorkQueue();
                 context.setAttribute("newJobs", newJobs);
                 System.out.println("OlaListener - Queue added");
                 context.setAttribute("keepWorking", Boolean.TRUE);
                 System.out.println("OlaListener - keepWorking");
                 JobConsumer jc = new JobConsumer();
                 System.out.println("OlaListener - Job Consumer created");
                 jc.setContext(context);
                 System.out.println("OlaListener - context set");
                 Thread t = new Thread(jc);
                 t.start();
    public class JobConsumer implements Runnable{
         /*log4j*/     private static final Logger logger = Logger.getLogger(JobConsumer.class);     
         private ServletContext context = null;
         public void run(){
                 System.out.println("JobConsumer - run Start");
                 processQueue(context);
                 System.out.println("JobConsumer - run End");
    }

  • Encoding Problem: non-Unicode Data to Unicode format of XI

    Hi SDN,
    I have a JDBC sender to SAP BW scenario. The database is MS SQL server. 
    The code page of db CP1CIAS
    Description:SQL Server Sort Order 52 on Code Page 1252 for non-Unicode Data
    Some fields with values like <b>ZAK&#x0;ADY TWORZYW SZTUCZNYCH</b> are failing in XI Mapping with error
    <b>Fatal Error: com.sap.engine.lib.xml.parser.Parser~
    XMLParser : #0 not allowed in Character data sections
    in the trace.</b>
    Please help how should i get over this code page errors. By installing this code page on XI server help?

    There is no such global setting, this is b/c your source has Unicode I trust, and the only one other thing to try would be this:
    Arthur My Blog

  • Broken Pipe with Non-blocking Socket

    Hello,
    I write a Unix Agent who connect on a Windows Server with socket...
    Working well on Linux but on Solaris my problem is:
    -When my agent is running before server at connection all seems OK: Connect, Select and Getsockopt but when I try to send data I have always EPIPE Signal but I can receive datas from server !
    - When my agent is strarting after the server all it's Ok
    I don't unserstand this appears on Solaris SPARC 8 and Solaris 9 Intel ...
    Please Help is there something special with non-blocking sockets on Solaris ?
    Thanks

    Can't help you much but what I would recommend is that you
    insure that your pipes are opened for both read/write, even
    though you are only going to read or write from it. This insures
    that the pipe does not close down when you hit EOF.

  • Non-blocking serialization

    Dear all,
    Does anybody know whether PI/XI supports the non-blocking serialization function?
    And in which version?
    If not, is there any work-around?
    Thanks a lot.
    best regards
    Jing

    HI
    Serialization plays an important role in distributing interdependent objects, especially when master data is being distributed.
    IDocs can be created, sent and posted in a specified order by distributing message types serially.
    Errors can then be avoided when processing inbound IDocs.
    Interdependent messages can be serially distributed in the following ways:
    Serialization by Object Type
    Serialization by Message Type
    Serialization at IDoc Level
    (not for IDocs from generated BAPI-ALE interfaces)
    Serialization at IDoc Level
    Use
    Delays in transferring IDocs may result in an IDoc containing data belonging to a specific object arriving at its destination before an "older" IDoc that contains different data belonging to the same object. Applications can use the ALE Serialization API to specify the order IDocs of the same message type are processed in and to prevent old IDocs from being posted if processing is repeated.
    ALE provides two function modules to serialize IDocs which the posting function module has to invoke:
    u2022 IDOC_SERIALIZATION_CHECK
    checks the time stamps in the serialization field of the IDoc header.
    u2022 IDOC_SERIAL_POST
    updates the serialization table.
    Check the following link:
    http://help.sap.com/saphelp_nw04s/helpdata/en/0b/2a66d6507d11d18ee90000e8366fc2/frameset.htm
    http://help.sap.com/saphelp_nw04s/helpdata/en/78/2175a751ce11d189570000e829fbbd/frameset.htm
    How to serialize IDoc XML messages fed into XI
    cheers

  • Using non blocking connect() call for SCTP sockets in Solaris10

    Hi,
    I have a problem with non blocking connect call on SCTP socket.
    I am using the sctp stack support in Solaris10.
    When the connect is successful, I can get the pollout event on the socket.
    But there is no event observed when the peer does not exist. In other words, I could not get the pollout event on connection failure. This logic works fine with TCP sockets on both Solaris and Suse10.
    I am working with SCTP one-to-one style sockets.
    Is there any way to handle this issue?
    Do I need to load any patch to resolve this issue?
    It will be great if I get a solution in this regard.
    Thanks in advance.
    Best Regards,
    Bipin.

    There are at least two problems here.
    A. In the receiver you should test for -1 from the read() immediately, rather than continue with the loop and try to write -1 bytes to the file.
    B. In the sender you are ignoring the return value of client.write(), which can be anything from 0 to the buffer length. If you get 0 you should wait for another OP_WRITE to trigger; if you get a 'short write' you need to retry it until you've got nothing left to write from the current buffer, before you read any more data. This is where the data is vanishing.

  • GZIPInputStream non-blocking

    Hello!
    I'm using Java NIO for a network app. I need to decompress GZIP compressed data and I'm wondering if I can do that in a non-blocking way to fit my architecture. As it is now, using PipedInputStream with GZIPInputStream will block until enough data is available to decompress anything. I would like it to return empty handed if there's not enough data yet. Any solution to this?
    //shadewind

    You need a way to send the length of the GZIP file with the data so the reader know how much data to read.

  • Non-blocking file selector?

    Running W98/Labview 5.1.1, I have a DAQ application that collects data and
    streams it to file. For this I would like to be able to change output
    files without breaking the DAQ loop. For some reason, however the DAQ loop
    occurence will time out the moment I open the file dialog, even though
    the dialog runs in it's own while loop and even in it's own sub VI with
    low priority. Any suggestions how I could get a non-blocking dialog box?
    TIA,
    Rudolf

    Hi Rudolf,
    take a look
    http://zone.ni.com/devzone/devzoneweb.nsf/opendoc?openagent&75C636329327A87B862568690074800A&cat=8ABEC12D4C0AA4A3862567AC00583899
    I hope it help you in many cases.
    Mike
    Rudolf Potucek wrote:
    > Running W98/Labview 5.1.1, I have a DAQ application that collects data and
    > streams it to file. For this I would like to be able to change output
    > files without breaking the DAQ loop. For some reason, however the DAQ loop
    > occurence will time out the moment I open the file dialog, even though
    > the dialog runs in it's own while loop and even in it's own sub VI with
    > low priority. Any suggestions how I could get a non-blocking dialog box?
    >
    > TIA,
    >
    > Rudolf

  • Non-Blocking Multicast Sockets in JDK 1.4?

    Hi,
    I've been trying to create non-blocking multicast sockets in JDK1.4, which essentially seems (at this stage) to boil down to the simpler problem of creating a DatagramChannel that uses MulticastSockets, or at least DatagramSockets that can join a Multicast group. Not having found any obvious way to do it, I created this extraordinary hack:
    package java.net; // Wicked, wicked!
    import java.io.*;
    public class MyDatagramSocket {
    public static void join(java.net.DatagramSocket socket, InetAddress addr)
    throws IOExceptio DatagramSocket ds = new DatagramSocket(port);
    ds.setReuseAddress(true);
    MyDatagramSocket.join(ds, InetAddress.getByName("224.0.0.104"));
    DatagramPacket dp = new DatagramPacket(array, 5000);
    ds.receive(dp);          /* READS FINE */
    n
    socket.impl.join(addr); // Uses knowledge of DatagramSocket culled from examining source to access DatagramSocketImpl
    Now I compile this, and drop the class file into my rt.jar files (in the JDK and the JRE), so that I can use MyDatagramSocket.join (DatagramSocket, InetAddress), which looks like it should work from code like this:
    try {
    int port = 58501;
    DatagramChannel dc = DatagramChannel.open();
    dc.socket().setReuseAddress(true);
    dc.socket().bind(new InetSocketAddress(port));
    MyDatagramSocket.join(dc.socket(), InetAddress.getByName("224.0.0.104"));
    byte [] array = new byte[5000];
    ByteBuffer bb = ByteBuffer.wrap(array);
    dc.receive(bb);
    System.out.println("Read from dc");
    } catch (Exception x) {
    x.printStackTrace();
    But it doesn't work - it just doesn't read. A simpler example is this:
    DatagramSocket ds = new DatagramSocket(port);
    ds.setReuseAddress(true);
    MyDatagramSocket.join(ds, InetAddress.getByName("224.0.0.104"));
    DatagramPacket dp = new DatagramPacket(array, 5000);
    ds.receive(dp);          /* READS FINE */
    So I know that my hack is working, but this fails:
    DatagramChannel dc = DatagramChannel.open();
    dc.socket().bind(new InetSocketAddress(port));
    dc.socket().setReuseAddress(true);
    MyDatagramSocket.join(dc.socket(), InetAddress.getByName("224.0.0.104"));
    DatagramPacket dp = new DatagramPacket(array, 5000);
    dc.socket().receive(dp);     /* NEVER READS */
    I've reduced the problem to one of the difference between a java.net.DatagramSocket - the standard DatagramSocket, and a sun.nio.ch.DatagramSocketAdaptor, which is what DatagramChannels seem to use.
    My questions are:
    a) Is there a proper way to do this, without my adding my own classes to java.net?
    b) If NO is the answer to a), any ideas what I'm doing wrong in my code?
    Many thanks for any assistance,
    Craig

    I've encountered the same problem in my code. The datagramChannel never receives incoming data. Doesn't matter the rate at which you send it or anything else. I don't see any way around this problem at the moment. If i find something i'll post it. Interesting enough, my friend who programs with C++ got non-blocking I/O with datagrams to work in windows. So this might just be java.

Maybe you are looking for