Tuesday, January 16, 2007

SCBCD Mock Questions - Message-Driven Bean Component Contract

1)What will happen when you try to compile the following message driven bean? Assume all packages are correctly imported.

1.   public class GatewayBean implements MessageDrivenBean, MessageListener {
2. public GatewayBean(){}
3. public void ejbCreate(){}
4. public void onMessage(TextMessage msg){}
5. public void ejbRemove(){}
6. public void setMessageDrivenContext(MessageDrivenContext ctx){}
7. }


1)Compilation error, incorrect declaration on line 3.
2)Compilation error, incorrect declaration on line 4.
3)The code compiles fine.
4)Compilation error, the GatewayBean class must be declared abstract.
5)Compilation error, the GatewayBean class must be declared final.

2)Which of the following is assumed to be the correct sequence of methods that are called in the lifecycle of a message-driven bean?

1)
2)
3)
4)
5)

3)Which of the following interfaces need to be implemented by a message driven bean? [Select all correct answers]

1)MessageDrivenBean
2)MessageDrivenbean
3)MessageListener
4)MessageDrivenContext
5)TextMessage

4)Which of the following methods will cause an IllegalStateException when used from within the message-driven bean onMessage() method? [Select all correct answers]

1)getEJBHome()
2)getRollbackOnly()
3)getUserTransaction()
4)isCallerInRole()
5)getCallerPrincipal()

5)Identify the correct message-driven bean method that can be used to access a bean managed entity bean?

1)ejbCreate()
2)message-driven bean constructor.
3)onMessage()
4)you cannot access a bean managed entity bean from a message-driven bean.
5)setMessageDrivenContext()

6)______ is thrown when the message bean invokes the isCallerInRole() method from the MessageDrivenContext interface. [Do not include the name of the package]

7)Which of the following statements about message driven beans are correct? [Select all correct answers]

1)The bean class must be public, final and must not be abstract.
2)The bean class must be public, not final and must not be abstract.
3)The bean class must have a non-argument ejbCreate method.
4)The bean class must only implement the MessageDrivenBean interface.
5)Methods in a message driven bean cannot throw a RuntimeException

8)Which of the following statements are true about acknowledgement of messages within message-driven beans? [Select all correct answers]

1)A message arrived in onMessage() is placed back on the message queue if the transaction rolls back for container managed message beans.
2)A message arrived in onMessage() within a container managed message bean is placed back on the message queue when the onMessage() method returns false.
3)A message arrived in onMessage() within a container managed message bean is placed back on the message queue when the onMessage() throws a checked exception.
4)A container managed message bean can use the deployment descriptor to define how messages are acknowledged to the message service.
5)The <acknowledge-mode> attribute defines the way bean managed message bean's send an acknowledgement to the message service when the transaction has failed.

9)Which of the following onMessage() declarations is correct in the message driven bean class?

1)public void onMessage(Message message)
2)public void onMessage(Message message) throws RemoteException
3)public String onMessage(Message message)
4)public String onMessage(Message message) throws RemoteException
5)public static void onMessage(Message message)

10)The ______ tag is used to indicate the acknowledgement mode used for bean managed message beans. [Enter the name of the tag, do not include the opening and closing braces]

Answers:

1)2

Answer 2 is correct. The message-driven bean class must define one onMessage method whose signature must follow these rules:

  • The method must be declared as public.
  • The method must not be declared as final or static.
  • The return type must be void.
  • The method must have a single argument of type javax.jms.Message.
  • The throws clause must not define any application exceptions.


2)2

Answer 2 is correct.

A message-driven bean instance's life starts when the container invokes newInstance() on the message-driven bean class to create a new instance. Next, the container calls setMessageDrivenContext() followed by ejbCreate() on the instance.

The message-driven bean instance is now ready to be delivered a message sent to its Destination by any client.

When the container no longer needs the instance (which usually happens when the container wants to reduce the number of instances in the method-ready pool), the container invokes ejbRemove() on it. This ends the life of the message-driven bean instance.



3)1,3

Answers 1 and 3 are correct. A message driven bean must implement directly or indirectly implement 2 interfaces.

The MessageListener interface is used to receive asynchronously delivered messages. This interface only defines the onMessage() method.

The MessageDrivenBean is used by the container to notify the enterprise Bean instances of the instance's life cycle events. The required methods are: setMessageDrivenContext and ejbRemove.



4)1,4,5

Answers 1, 4 and 5 are correct.

Invoking getEJBHome() or getEJBLocalHome() is disallowed in message-driven bean methods because there are no EJBHome or EJBLocalHome objects for message-driven beans. The Container will throw and log the java.lang.IllegalStateException if these methods are invoked.

Invoking the getCallerPrincipal() and isCallerInRole() methods is disallowed in the message-driven bean methods because the Container does not have a client security context. The Container will throw and log the java.lang.IllegalStateException if either of these methods is invoked.

Answer 2 is incorrect, the message-driven bean can use the method getRollbackOnly() without causing any problems. However an IllegalStateException is thrown when the method is invoked while the instance is not associated with a valid transaction.

Answer 3 is incorrect, the UserTransaction interface is unavailable to message-driven beans with container-managed transaction demarcation.



5)3

Answer 3 is correct, this is the only method except from your business methods that has a meaningful transaction context.

Answer 1 is incorrect, the only operations that are allowed from within the ejbCreate() method is to lookup your JNDI environment.

Answer 2 is incorrect, your constructor is left empty in most cases, you don't have a meaningful transaction context or a message driven context. Answer 4 is incorrect, you can access a BMP or a CMP entity bean or a session bean from within your message-driven bean. Answer 5 is incorrect, the only allowed operations from the setMessageDrivenContext() is to lookup your JNDI environment.

The onMessage() method can be used to access the resource manager, access to other enterprise bean's, using the getRollBackOnly() and setRollbackOnly() methods from the MessageDrivenContext and searching your JNDI environment. Bean managed beans can use also the userTransaction context.



6)

IllegalStateException is the exception that the container will throw when you use the isCallerInRole() method from the MessageDrivenContext interface.

Invoking the isCallerInRole() method is disallowed in the message-driven bean methods because the container does not have a client security context.


7)2,3

Answers 2 and 3 are correct. Answer 1 is incorrect, the class implementing the MessageDrivenBean interface cannot be final.

Answer 4 is incorrect, the message driven bean must implement the MessageDrivenBean interface and the MessageListener interface directly or indirectly.

Answer 5 is incorrect, methods defined in the message driven bean can throw a RuntimeException, they can however not throw checked exceptions. However, it is a better practice not to throw a RuntimeException.

Learn more...

The following are the requirements for the message-driven bean class:

  • The class must implement, directly or indirectly, the javax.ejb.MessageDrivenBean interface.
  • The class must implement, directly or indirectly, the javax.jms.MessageListener interface.
  • The class must be defined as public, must not be final, and must not be abstract.
  • The class must have a public constructor that takes no arguments. The Container uses this constructor to create instances of the message-driven bean class.
  • Each message-driven bean class must have one ejbCreate method, with no arguments.
  • The class must not define the finalize() method.


8)1,5

Answer 1 and 5 are correct.

Bean managed message beans will acknowledge when the onMessage() method completes. Bean managed message beans has 2 choices how to acknowledge, this depends on the deployment descriptor. Container managed message beans will acknowledge depending on the status of the transaction.

Answer 2 is incorrect, the onMessage() method cannot return false, as it signature is a void. Container managed message beans acknowledge to the message service depending on the status of the transaction. Answer 3 is incorrect, message driven beans does not throw checked exceptions. Answer 4 is incorrect, only bean managed message beans will use the configuration inside the deployment descriptor for acknowledgement.



9)1

Answer 1 is correct.

The correct declaration is: public void onMessage(Message message)

This method is defined in the javax.jms.MessageListener interface, the interface need to be implemented by the message driven bean together with the MessageDrivenBean interface.

All the methods in the message driven bean class will not throw a RemoteException, a message driven bean does not a have a client view, not locally and not remotely.



10)

acknowledge-mode is the correct answer.

If bean managed transaction demarcation is used, the message receipt cannot be part of the bean-managed transaction, and, in this case, the receipt is acknowledged by the container. If bean managed transaction demarcation is used, the Bean Provider can indicate in the acknowledge-mode deployment descriptor element whether JMS AUTO_ACKNOWLEDGE semantics or DUPS_OK_ACKNOWLEDGE semantics should apply. If the acknowledge-mode deployment descriptor element is not specified, JMS AUTO_ACKNOWLEDGE semantics are assumed.

No comments: