|
35 | 35 | import jakarta.jms.JMSException; |
36 | 36 | import jakarta.jms.Message; |
37 | 37 | import jakarta.jms.MessageConsumer; |
| 38 | +import jakarta.jms.MessageFormatException; |
38 | 39 | import jakarta.jms.MessageListener; |
39 | 40 | import jakarta.jms.TransactionRolledBackException; |
40 | 41 |
|
@@ -713,6 +714,144 @@ public Message receiveNoWait() throws JMSException { |
713 | 714 | return createActiveMQMessage(md); |
714 | 715 | } |
715 | 716 |
|
| 717 | + /** |
| 718 | + * Receives the next message produced for this message consumer and returns |
| 719 | + * its body as an object of the specified type. This call blocks |
| 720 | + * indefinitely until a message is produced or until this message consumer |
| 721 | + * is closed. |
| 722 | + * <p> |
| 723 | + * If the message is not of a type for which the body can be assigned to |
| 724 | + * the specified type, a {@code MessageFormatException} is thrown and the |
| 725 | + * message is not acknowledged. It may be delivered again when a subsequent |
| 726 | + * {@code receive} or {@code receiveBody} call is made. |
| 727 | + * |
| 728 | + * @param c the type to which the body of the next message should be |
| 729 | + * assigned |
| 730 | + * @return the body of the next message, or null if this message consumer |
| 731 | + * is concurrently closed |
| 732 | + * @throws MessageFormatException if the message body cannot be assigned to |
| 733 | + * the specified type |
| 734 | + * @throws JMSException if the JMS provider fails to receive the next |
| 735 | + * message due to some internal error |
| 736 | + */ |
| 737 | + public <T> T receiveBody(Class<T> c) throws JMSException { |
| 738 | + checkClosed(); |
| 739 | + checkMessageListener(); |
| 740 | + |
| 741 | + sendPullCommand(0); |
| 742 | + MessageDispatch md = dequeue(-1); |
| 743 | + if (md == null) { |
| 744 | + return null; |
| 745 | + } |
| 746 | + |
| 747 | + return doReceiveBody(md, c); |
| 748 | + } |
| 749 | + |
| 750 | + /** |
| 751 | + * Receives the next message produced for this message consumer and returns |
| 752 | + * its body as an object of the specified type, blocking up to the |
| 753 | + * specified timeout. A {@code timeout} of zero never expires and the call |
| 754 | + * blocks indefinitely. |
| 755 | + * <p> |
| 756 | + * If the message is not of a type for which the body can be assigned to |
| 757 | + * the specified type, a {@code MessageFormatException} is thrown and the |
| 758 | + * message is not acknowledged. It may be delivered again when a subsequent |
| 759 | + * {@code receive} or {@code receiveBody} call is made. |
| 760 | + * |
| 761 | + * @param c the type to which the body of the next message should be |
| 762 | + * assigned |
| 763 | + * @param timeout the timeout value (in milliseconds), a timeout of zero |
| 764 | + * never expires |
| 765 | + * @return the body of the next message, or null if the timeout expires or |
| 766 | + * this message consumer is concurrently closed |
| 767 | + * @throws MessageFormatException if the message body cannot be assigned to |
| 768 | + * the specified type |
| 769 | + * @throws JMSException if the JMS provider fails to receive the next |
| 770 | + * message due to some internal error |
| 771 | + */ |
| 772 | + public <T> T receiveBody(Class<T> c, long timeout) throws JMSException { |
| 773 | + checkClosed(); |
| 774 | + checkMessageListener(); |
| 775 | + if (timeout == 0) { |
| 776 | + return this.receiveBody(c); |
| 777 | + } |
| 778 | + |
| 779 | + sendPullCommand(timeout); |
| 780 | + while (timeout > 0) { |
| 781 | + MessageDispatch md; |
| 782 | + if (info.getPrefetchSize() == 0) { |
| 783 | + md = dequeue(-1); |
| 784 | + } else { |
| 785 | + md = dequeue(timeout); |
| 786 | + } |
| 787 | + |
| 788 | + if (md == null) { |
| 789 | + return null; |
| 790 | + } |
| 791 | + |
| 792 | + return doReceiveBody(md, c); |
| 793 | + } |
| 794 | + return null; |
| 795 | + } |
| 796 | + |
| 797 | + /** |
| 798 | + * Receives the next message produced for this message consumer and returns |
| 799 | + * its body as an object of the specified type if one is immediately |
| 800 | + * available. |
| 801 | + * <p> |
| 802 | + * If the message is not of a type for which the body can be assigned to |
| 803 | + * the specified type, a {@code MessageFormatException} is thrown and the |
| 804 | + * message is not acknowledged. It may be delivered again when a subsequent |
| 805 | + * {@code receive} or {@code receiveBody} call is made. |
| 806 | + * |
| 807 | + * @param c the type to which the body of the next message should be |
| 808 | + * assigned |
| 809 | + * @return the body of the next message, or null if one is not immediately |
| 810 | + * available |
| 811 | + * @throws MessageFormatException if the message body cannot be assigned to |
| 812 | + * the specified type |
| 813 | + * @throws JMSException if the JMS provider fails to receive the next |
| 814 | + * message due to some internal error |
| 815 | + */ |
| 816 | + public <T> T receiveBodyNoWait(Class<T> c) throws JMSException { |
| 817 | + checkClosed(); |
| 818 | + checkMessageListener(); |
| 819 | + sendPullCommand(-1); |
| 820 | + |
| 821 | + MessageDispatch md; |
| 822 | + if (info.getPrefetchSize() == 0) { |
| 823 | + md = dequeue(-1); |
| 824 | + } else { |
| 825 | + md = dequeue(0); |
| 826 | + } |
| 827 | + |
| 828 | + if (md == null) { |
| 829 | + return null; |
| 830 | + } |
| 831 | + |
| 832 | + return doReceiveBody(md, c); |
| 833 | + } |
| 834 | + |
| 835 | + /** |
| 836 | + * Checks that the message body can be assigned to the requested type, |
| 837 | + * acknowledges the message, and returns its body. If the body cannot be |
| 838 | + * assigned, the message is re-enqueued without acknowledgement so that it |
| 839 | + * remains available for a subsequent {@code receive} or |
| 840 | + * {@code receiveBody} call. |
| 841 | + */ |
| 842 | + private <T> T doReceiveBody(MessageDispatch md, Class<T> c) throws JMSException { |
| 843 | + ActiveMQMessage message = createActiveMQMessage(md); |
| 844 | + if (!message.isBodyAssignableTo(c)) { |
| 845 | + // spec: message is not acknowledged and left for redelivery |
| 846 | + unconsumedMessages.enqueueFirst(md); |
| 847 | + throw new MessageFormatException("Message body cannot be read as type: " + c); |
| 848 | + } |
| 849 | + |
| 850 | + beforeMessageIsConsumed(md); |
| 851 | + afterMessageIsConsumed(md, false); |
| 852 | + return message.getBody(c); |
| 853 | + } |
| 854 | + |
716 | 855 | /** |
717 | 856 | * Closes the message consumer. |
718 | 857 | * <P> |
|
0 commit comments