代写 Scheme Java database (19F)CST8277 Enterprise Application Programming

(19F)CST8277 Enterprise Application Programming
Assignment 1: Ticket Management System
Please read this document carefully, all sections (perhaps multiple times to make sure you find all the places that say ‘… you must’) If your submission does not meet the requirements as stated here, you may lose marks even though your program runs. Additionally, this assignment is also a teaching opportunity – (some) material presented here will be on the Midterm or Final exam
Theme for Assignment 1
After completing this assignment, you will have achieved the following:
1. Create a program to demonstrate Java Multithreading
2. Gain experience in implementing a solution to a ‘classic’ Producer/Consumer problem
3. Gain experience in implementing a (thread-safe) Concurrent Collection class as well as
using a JDK provided class
4. Use Hibernate in a thread-safe manner
5. Create a test plan for your solution
Grading for Assignment 1
There is a Brightspace Rubric that describes the marking scheme for this assignment:

You must provide:
• Short Answers to the questions from Section 2
• Code – complete Eclipse (Maven) project that compiles, has good indenting, not overly
complicated, is efficient.
• Code concepts – demonstrates understanding of how Java Multithreading works
• Code style – every class and every class member field has Javadoc comments; every
class file has a multiline comment block at the top giving the name of the file, your name (author), creation date

o Important-yournamemustappearatthetopofeachandeverysourcecode file submitted; otherwise, you will lose marks (up to a score of 0) for the coding portion of the Rubric.
o CodedescribedinthisdocumentandonBrightspace 19FAssignment1_starter.zip must be used to provide your solution. You will notice the first time you import it into Eclipse, not everything will compile or work – that is done on purpose, you must rename the project to ‘Assignment1_ NNNNNNN’ (whatever your AC student number is), fix the errors and get the code to work.
o .Javadocs: the Javadocs in the starter zip are good examples to follow
• Test Plan – this is a paper test plan, not actual running tests. Please submit a Word
document with a table in the following format: o Expectedtemplate:
Description
Pre- Conditions
Post- Conditions
Test methodology
Expected
Actual
Notes
Are operations taking place asynchrono usly
Consumer started, ready to process records. Producer started, submitting records to be consumed
Records successfully added and removed from (concurrent) collection
Viewing output logs
Should see Producer/Co nsumer messages in tandem
Matches expected
Test Passe s
o DescribetestsforTicketConsumerclass,TicketProducerclassandthetwo buffer classes – minimum 6 test cases (rows) in total
o Important-notfollowingthetemplatewillresultinlossofmarks(uptoascoreof zero for that portion of the Rubric)
Section 1 – Solving the ‘classic’ Producer/Consumer problem using Java Multithreading and Thread-safe Collections
The focus is on the ‘classic’ Computer Science problem called ‘Producer/Consumer’ (Wikipedia link). The variation for this assignment is:
• Multiple Producers, Single Consumer: work is processed in strict arrival-order – each request is marked with a timestamp before entering the database, which is important to customers of the Ticket system, knowing that tickets are being processed fairly.

Step 0 – Read Deitel (chapter 23.5, 11th Ed) ‘Producer/Consumer Relationship with Synchronization’
While reading the textbook material think about “How do I implement a CircularBuffer?” and “How do I use the ArrayBlockingQueue?” (and again, think … Midterm questions!)
Step 1 – Starter code
Included in the Assignment 1 starter material on Brightspace is an Eclipse (Maven) project with some code to use to build your solution. The classes are modelled after those presented in the Deitel textbook:
public interface Buffer {
/**
* A method to add an Element to the buffer. *
* @param element – the object to be added.
* @throws InterruptedException – if the method is interrupted
while wait()-ing.
*/
public void blockingPut(E element) throws InterruptedException;
/**
* A method to remove an Element from the buffer. *
* @return – the next object in the buffer.
* @throws InterruptedException – if the method is interrupted
while wait()-ing.
*/
public E blockingGet() throws InterruptedException; }
You must create two different implementations of the Buffer interface – the starter file contains partially filled-in .java files:

CircularBuffer :
public class CircularBuffer implements Buffer { protected E[] bufArray;
@SuppressWarnings(“unchecked”)
public CircularBuffer(int capacity) {
bufArray = (E[])new Object[capacity]; }
public void blockingPut(E element) throws InterruptedException { throw new RuntimeException(“Method not implemented”);
}
public E blockingGet() throws InterruptedException { throw new RuntimeException(“Method not implemented”);
} }
The Deitel chapter walks through creating a Circular buffer implementation – you must adapt what you learn from the above class (and again, think … Midterm questions!).
BlockingQueueBuffer :
public class BlockingQueueBuffer implements Buffer { // TODO – member fields
public BlockingQueueBuffer(int capacity) {
throw new RuntimeException(“constructor not implemented”);
}
public void blockingPut(E element) throws InterruptedException { throw new RuntimeException(“method not implemented”);
}
public E blockingGet() throws InterruptedException { throw new RuntimeException(“method not implemented”);
} }
The BlockingQueueBuffer class must use an existing JDK class from the java.util.concurrent package: ArrayBlockingQueue . Please study the Javadocs for the class so you understand how to use it in the above class.
Step 2 – Create TicketProducer and TicketConsumer classes
Under the src/main/java… folders there are definitions of Java Interfaces very similar to those found in the Deitel chapter:

public interface Producer extends Runnable { int produce();
/**
* This method is called when an instance of this {@link Runnable} to
installed in a {@link Thread} *
* @author mwnorman
*/
default void run() {
int numProduced = produce();
} }
public class TicketProducer implements Producer> { …
@Override
public int produce() { …
} }
(the Consumer class is very similar) Why use default methods?
What is different from the code presented in the (e)textbook? Java 8 introduced a new language capability: Interfaces, which previously only contained signatures of methods, can now contain code for default methods. If a class implements the interface but does not provide an overriding implementation, then the code in the default method is used. Above we see the Producer interface extending Runnable and redirecting calls to run() to an as-yet-not-implemented produce() method. For most of Java’s history the language was static, changing only very slowly. More recently, push-back from the Java community has accelerated the introduction of new features. Some of these features solve what Programming Language Theorists (yes, there are such folk as Programming Language Theorists!) label ceremony. A programming language should allow the clear expression of the essence of a program without being caught up in excessive ceremony to express it without extraneous clutter. Prior to Java 8, the above would require two Java classes:
public interface Producer extends Runnable { int produce();
}

public abstract ProducerBase implements Producer {
@Override
public void run() {
int numProduced = produce();
} }
The TicketProducer class would then need to extend ProducerBase. That is a lot of ceremony to just redirect a single method – default methods really help.
TicketProducer
The job of the TicketProducer class is to create random Ticket objects (using the PODAM library) and place them using the blockingPut() API onto the buffer shared amongst TicketProducer and TicketConsumer threads.
TicketConsumer
The job of the TicketConsumer class is to remove Ticket objects from the buffer and insert them into the H2 tickets Database. This class must use Hibernate to do this in a thread-safe manner. Recall the two Hibernate artifacts: SessionFactory and Session – which is thread- safe and which is not?
The Java enum class HibernateHelper (partially) implements some useful code (saveTicket and in the HibernateHelper constructor). When an Java enum class has only a single enum type field (in this case INSTANCE), it is typically being used to implement the Singleton pattern (Wikibooks link) You will need to finish the code in the constructor to build a SessionFactory .
MultipleProducersSingleConsumer
Back under the src/test/java folders there is the MultipleProducersSingleConsumer class. Once you have corrected enough “red-(X)’s” in Eclipse, run the class with the argument –help:

2019-08-18T23:52:39.442Z INFO [main] c.a.c.a.MultipleProducersSingleConsumer – Usage:
-h (–help)
-b (–buffer)
-s (–size)
-p (–producer) -t (–tickets)
: print this message (default: true)
: b=BlockingQueue Buffer, c=Circular Buffer : buffer size (default: 20)
: number of producer threads (default: 4) : number of tickets (default: 1000)
The job of this class is to put everything together: reading the command-line arguments, using the buffer implementations, Runnable/Callable classes, etc.

Section 2 – Short Questions
Question 1 – Which implementation of the Buffer was easier to code?
1. The CircularBuffer using the synchronized keyword with methods wait() and notify()
2. The buffer using ArrayBlockingQueue
It is important to note that a one-line answer “I found Whatever class easier to code” is not
sufficient – you must explain why
Additionally please keep your answer consise and to the point … adding a lot of words tends to make me think of the “throw-it-against-the-wall-and-hope-something-sticks” approach, indicating you did not really understand ,but are hoping enough ‘stuff’ sticks to the wall to be rewarded with pointsL
Question 2 – Ease of implementation aside, what are some advantages and disadvantages of each approach?
(Please refer to above comment about how consise to-the-point answers are better)