Andrew Borg. The University of York. 1 Java NIO Andrew Borg.

Slides:



Advertisements
Similar presentations
New I/O (JSR 51) Robert Rock Howard Chief Technology Officer Tower Technology.
Advertisements

What is RMI? Remote Method Invocation –A true distributed computing application interface for Java, written to provide easy access to objects existing.
Java Network Programming Vishnuvardhan.M. Dept. of Computer Science - SSBN Java Overview Object-oriented Developed with the network in mind Built-in exception.
RMI Varun SainiYing Chen. What is RMI? RMI is the action of invoking a method of a remote interface on a remote object. It is used to develop applications.
CS 443 Advanced OS Fabián E. Bustamante, Spring 2005 Resource Containers: A new Facility for Resource Management in Server Systems G. Banga, P. Druschel,
Remote Method Invocation (RMI) Mixing RMI and sockets
Study of Hurricane and Tornado Operating Systems By Shubhanan Bakre.
Copyright © 2001 Qusay H. Mahmoud RMI – Remote Method Invocation Introduction What is RMI? RMI System Architecture How does RMI work? Distributed Garbage.
Lab#1 (14/3/1431h) Introduction To java programming cs425
The road to reliable, autonomous distributed systems
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
OS2-1 Chapter 2 Computer System Structures. OS2-2 Outlines Computer System Operation I/O Structure Storage Structure Storage Hierarchy Hardware Protection.
Architectural Support for Operating Systems. Announcements Most office hours are finalized Assignments up every Wednesday, due next week CS 415 section.
Introduction to Remote Method Invocation (RMI)
Chapter 4: Threads Adapted to COP4610 by Robert van Engelen.
Practical Session 11 Multi Client-Server Java NIO.
1 Developing Native Device for MPJ Express Advisor: Dr. Aamir Shafi Co-advisor: Ms Samin Khaliq.
Chapter 6 Operating System Support. This chapter describes how middleware is supported by the operating system facilities at the nodes of a distributed.
Apache Mina Dima Ionut Daniel. Contents What is Apache Mina? Why Apache Mina? Mina Architecture Mina Core Mina Advanced JMX Support Spring Integration.
CS 390- Unix Programming Environment CS 390 Unix Programming Environment Topics to be covered: Distributed Computing Fundamentals.
Distributed Real- Time in the RTSJ Andrew Borg. Presentation RMI – A brief introduction The RTSJ – A (very) brief introduction The DRTSJ – The 3 Levels.
Chapter 3.5 Memory and I/O Systems. 2 Memory Management Memory problems are one of the leading causes of bugs in programs (60-80%) MUCH worse in languages.
1 Introduction to Middleware. 2 Outline What is middleware? Purpose and origin Why use it? What Middleware does? Technical details Middleware services.
Proactor Pattern Venkita Subramonian & Christopher Gill
1 Concurrency Architecture Types Tasks Synchronization –Semaphores –Monitors –Message Passing Concurrency in Ada Java Threads.
Java Programming: Advanced Topics 1 Input/Output and Serialization Chapter 3.
 Remote Method Invocation  A true distributed computing application interface for Java, written to provide easy access to objects existing on remote.
Practical Session 12 Reactor Pattern. Disadvantages of Thread per Client It's wasteful – Creating a new Thread is relatively expensive. – Each thread.
Hwajung Lee.  Interprocess Communication (IPC) is at the heart of distributed computing.  Processes and Threads  Process is the execution of a program.
CORBA Overview Distributed Systems &Middleware ICS243f 22 November 2015 Arvind S. Krishna Info & Comp Science Dept University of California, Irvine
Practical Session 11 Multi Client-Server Java NIO.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
CSCI-383 Object-Oriented Programming & Design Lecture 10.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 4 Computer Systems Review.
JS (Java Servlets). Internet evolution [1] The internet Internet started of as a static content dispersal and delivery mechanism, where files residing.
Today… “Hello World” ritual. Brief History of Java & How Java Works. Introduction to Java class structure. But first, next slide shows Java is No. 1 programming.
Java Network Programming Network Programming Spring 2000 Jeffrey E. Care
Java – in context Main Features From Sun Microsystems ‘White Paper’
E81 CSE 532S: Advanced Multi-Paradigm Software Development Venkita Subramonian, Christopher Gill, Ying Huang, Marc Sentany Department of Computer Science.
Written by: Dr. JJ Shepherd
Distributed File Systems Questions answered in this lecture: Why are distributed file systems useful? What is difficult about distributed file systems?
Real world OS programming Nezer J. Zaidenberg.  Create process/thread  File and directory I/O  I/O multiplexing  Sync and locking  Access system.
Java Programming: Advanced Topics 1 Input/Output and Serialization.
SPL/2010 Reactor Design Pattern 1. SPL/2010 Overview ● blocking sockets - impact on server scalability. ● non-blocking IO in Java - java.niopackage ●
Lecture 1 Page 1 CS 111 Summer 2013 Important OS Properties For real operating systems built and used by real people Differs depending on who you are talking.
Adding Concurrency to a Programming Language Peter A. Buhr and Glen Ditchfield USENIX C++ Technical Conference, Portland, Oregon, U. S. A., August 1992.
Event Sources and Realtime Actions
Object Oriented Programming in
Advanced Topics in Concurrency and Reactive Programming: Asynchronous Programming Majeed Kassis.
SOFTWARE DESIGN AND ARCHITECTURE
What is RMI? Remote Method Invocation
CHAPTER 5 JAVA FILE INPUT/OUTPUT
Netty.
Reactor Design Pattern
Pluggable Architecture for Java HPC Messaging
CSE 451: Operating Systems Autumn 2003 Lecture 16 RPC
Chapter 40 Remote Method Invocation
Chapter 46 Remote Method Invocation
Chapter 46 Remote Method Invocation
Why Threads Are A Bad Idea (for most purposes)
Why Threads Are A Bad Idea (for most purposes)
CSE 451: Operating Systems Winter 2003 Lecture 16 RPC
Why Threads Are A Bad Idea (for most purposes)
IO Package in Java.
The reactor design pattern
Thread per client and Java NIO
Chapter 13: I/O Systems “The two main jobs of a computer are I/O and [CPU] processing. In many cases, the main job is I/O, and the [CPU] processing is.
Message Passing Systems
Presentation transcript:

Andrew Borg. The University of York. 1 Java NIO Andrew Borg

Andrew Borg. The University of York.2 This Presentation: What NIO provides A short “tutorial-like” introduction to NIO On NIO and patterns for distributed systems Why NIO is important to us:  Direct Memory Access and the RTSJ  Nonblocking IO in RMI

Andrew Borg. The University of York.3 This Presentation: What NIO provides A short “tutorial-like” introduction to NIO On NIO and patterns for distributed systems Why NIO is important to us

Andrew Borg. The University of York.4 What is Java NIO? Stands for New IO. Started off as a JSR under Sun’s JCP (JSR51). “APIs for scalable I/O, fast buffered binary and character I/O, regular expressions, charset conversion, and an improved filesystem interface.”

Andrew Borg. The University of York.5 How does it do it? An API for scalable I/O operations on both files and sockets, in the form of either asynchronous requests or polling; An API for fast buffered binary I/O, including the ability to map files into memory when that is supported by the underlying platform; An API for fast buffered character I/O, including a simple parsing facility based upon regular expressions and a simple printf-style formatting facility;

Andrew Borg. The University of York.6 …and An API for character-set converters, including a service- provider interface for pluggable converters A rich yet platform-independent set of I/O exceptions A new filesystem interface that supports bulk access to file attributes (including MIME content types), escape to filesystem-specific APIs, and a service-provider interface for pluggable filesystem implementations

Andrew Borg. The University of York.7 JDK 1.4.x Provides the package java.nio This package is a subset of JSR-51. In particular, proposed file-handling functionality is absent

Andrew Borg. The University of York.8 Why so long for good IO? …Because it is hard for a platform independent development platform to provide functionality common to all platforms Not all OS’ provide the same functions. IO depends very much on the underlying OS. For example file metadata is OS specific Many of the IO features require native code

Andrew Borg. The University of York.9 So what does NIO give us? A lot – but not everything Native code is still required – but you lose portability …but some of the things that could be done before only with native code are now provided in NIO

Andrew Borg. The University of York.10 This Presentation: What NIO provides A short “tutorial-like” introduction to NIO On NIO and patterns for distributed systems Why NIO is important to us

Andrew Borg. The University of York.11 The java.nio API java.nio  java.nio.*  java.nio.charset and java.nio.charset.spi  java.nio.channels and java.nio.channels.spi  (java.util.regex) Minor Changes in other packages

Andrew Borg. The University of York.12 java.nio.* A whole set of abstract buffers classes, all extending the abstract class java.nio.buffer eg: ByteBuffer, CharBuffer, DoubleBuffer, FloatBuffer, IntBuffer, LongBuffer, ShortBuffer Set of methods: reset(), flip(), etc. 0 <= mark <= position <= limit <= capacity

Andrew Borg. The University of York.13 So what is special about these buffers? Direct allocation to physical memory is allowed using native methods … “if possible” May be mapped to a file (memory-mapped files). An implementation may use a backing array. Different “views” for byte buffers JNI methods

Andrew Borg. The University of York.14 JNI functions for NIO direct buffers jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity); void* GetDirectBufferAddress(JNIEnv* env, jobject buf); jlong GetDirectBufferCapacity(JNIEnv* env, jobject buf);

Andrew Borg. The University of York.15 Memory and the RTSJ The RTSJ had to think about this too.  RawMemoryAccess  VT and LT PhysicalMemory are mapped to physical addresses public LTPhysicalMemory(java.lang.Object type, long size) throws … So what should we do; in particular for RawMemoryAccess?

Andrew Borg. The University of York.16 java.nio.charset Package java.nio.charset.spi  Service-provider classes for the java.nio.charset package Package java.nio.charset  Encoders and Decoders for difference character sets

Andrew Borg. The University of York.17 //convert ISO encoded bytes in a ByteBuffer to a string in a CharBuffer and visa versa // Create the encoder and decoder for ISO Charset charset = Charset.forName("ISO "); CharsetDecoder decoder = charset.newDecoder(); CharsetEncoder encoder = charset.newEncoder(); try { ByteBuffer bbuf = encoder.encode(CharBuffer.wrap("a string")); CharBuffer cbuf = decoder.decode(bbuf); String s = cbuf.toString(); } catch (CharacterCodingException e) { }

Andrew Borg. The University of York.18 java.nio.channels Selector provider in.channels.spi provides the abstract class by which the Java virtual machine maintains a single system-wide default provider instance The provider provides instances of DatagramChannel, Pipe, Selector, ServerSocketChannel, and SocketChannel

Andrew Borg. The University of York.19 Selector Selectable Channel Selection Key Selectable Channel register

Andrew Borg. The University of York.20 Selectors A selector maintains three sets of selection keys  The key set  The selected-key set  The cancelled-key Event driven – block for events on selection Selection is done by select(), selectNow() and select(int timeout). Returns the selected key set.

Andrew Borg. The University of York.21 Selection Keys A selection key is created each time a channel is registered with a selector A selection key contains two operation sets  Interest Set  Ready Set

Andrew Borg. The University of York.22 Socket Channels Only one operation is valid for ServerSocketChannles: SelectionKey.OP_ACCEPT SocketChannels support connecting, reading, and writing, so this method returns (SelectionKey.OP_CONNECT | SelectionKey.OP_READ | SelectionKey.OP_WRITE)

Andrew Borg. The University of York.23 Concurrency and NIO Selectors are thread safe but selection keys are not Care must be taken to avoid deadlock To use NIO you don’t have to be brilliant at socket programming – but you have to know your concurrency and race-condition resolution strategies

Andrew Borg. The University of York.24 Concurrency Example Selectable Channel select() If you: (1)Add a Channel (2)Cancel a channel (3) Modify the interestOps for a channel The selector MUST be awake. If it is blocked on a select(), you will also block.

Andrew Borg. The University of York.25 Solving concurrency issues Issuing a wakeUp() to nudge the selector  This method is similar to notify() but it is a “queued” notify Queuing requests so that on unblocking of the selector, all required ops are added It is a better idea to use one thread to wait on a selector

Andrew Borg. The University of York.26 This Presentation: What NIO provides A short “tutorial-like” introduction to NIO On NIO and patterns for distributed systems Why NIO is important to us

Andrew Borg. The University of York.27 A new platform for Scalable Distributed Computing Development Previously, distributed Java applications were not scalable – when using RMI or sockets Technologies like JINI provide scalable architectures but not scalable servers per se – the goals are different.

Andrew Borg. The University of York.28 Distributed Patterns with NIO Reactor Pattern:  Implemented in a couple of days  Most time spent on fixing concurrency Leaders followers

Andrew Borg. The University of York.29 NIO and ACE Similar idea ACE reactor pattern also includes timers. NIO does not but includes anything that can be described as selectable (eg. pipes) How you implement the pattern is up to you

Andrew Borg. The University of York.30 This Presentation: What NIO provides A short “tutorial-like” introduction to NIO On NIO and patterns for distributed systems Why NIO is important to us

Andrew Borg. The University of York.31 DRTSJ and RMI Seeks to extend the RTSJ with a real-time RMI for development of Distributed RT 2 approaches – Non-distributed and distributed thread model The engineering of any approach is hard

Andrew Borg. The University of York.32 TAO, ACE and RMI We are giving a good look at TAO and ACE and the approach taken in RT- CORBA How to migrate some aspect of these with RMI

Andrew Borg. The University of York.33 The problems with RMI There is no concept of a POA There is no way to configure an acceptance mechanism Real-time systems cannot allow arbitrary threading policies JSR 78 Custom Remote References was a step in the right direction but was rejected

Andrew Borg. The University of York.34 RMI and NIO Currently it is hard (or impossible) to implement a scalable RMI using the standard JDK Sun’s RMI implementation creates a thread for each connection but then tries to use those connections intelligently Strictly speaking you only need one connection per end-point to any endpoint In real-time, this is not going to work Zen developers took up Half-Sync Half-Async pattern as it is the only thing that Java would offer

Andrew Borg. The University of York.35 A new scalable RMI Make use of NIO to handle connections Use a thread-pool rather than create a new thread for every connection Still no application control if to be conformant with the spec

Andrew Borg. The University of York.36 That new scalable RMI and the RTSJ Scoped memory will make it hard. In particular:  How should scoped memory be used in any acceptance and handling pattern  How does this effect how applications would have to be developed Perhaps some patterns are just inappropriate in the presence of scopes?

Andrew Borg. The University of York.37 Conclusion Useful and long awaited addition to Java Will make message-passing applications more scalable Will make RMI implementations more scalable However, will still be useless for RT unless appropriate steps are taken in RMI