|
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 |
|
|
43 | 44 | import org.apache.activemq.command.ActiveMQDestination; |
44 | 45 | import org.apache.activemq.command.ActiveMQMessage; |
45 | 46 | import org.apache.activemq.command.ActiveMQObjectMessage; |
| 47 | +import org.apache.activemq.command.ActiveMQStreamMessage; |
46 | 48 | import org.apache.activemq.command.ActiveMQTempDestination; |
47 | 49 | import org.apache.activemq.command.CommandTypes; |
48 | 50 | import org.apache.activemq.command.ConsumerId; |
@@ -713,6 +715,221 @@ public Message receiveNoWait() throws JMSException { |
713 | 715 | return createActiveMQMessage(md); |
714 | 716 | } |
715 | 717 |
|
| 718 | + /** |
| 719 | + * Receives the next message produced for this message consumer and returns |
| 720 | + * its body as an object of the specified type. This call blocks |
| 721 | + * indefinitely until a message is produced or until this message consumer |
| 722 | + * is closed. |
| 723 | + * <p> |
| 724 | + * If the message is not of a type for which the body can be assigned to |
| 725 | + * the specified type, a {@code MessageFormatException} is thrown. The |
| 726 | + * subsequent behaviour depends on the session's acknowledge mode: |
| 727 | + * <ul> |
| 728 | + * <li>{@code AUTO_ACKNOWLEDGE} / {@code DUPS_OK_ACKNOWLEDGE}: the |
| 729 | + * message is not acknowledged and will be delivered again before any |
| 730 | + * subsequent messages. This is not considered redelivery and does not |
| 731 | + * cause the {@code JMSRedelivered} header or |
| 732 | + * {@code JMSXDeliveryCount} property to be updated.</li> |
| 733 | + * <li>{@code CLIENT_ACKNOWLEDGE}: the message is treated as delivered. |
| 734 | + * The application must call {@code session.recover()} to have it |
| 735 | + * redelivered.</li> |
| 736 | + * <li>Transacted session: the message is treated as delivered within the |
| 737 | + * transaction. The application must call {@code session.rollback()} |
| 738 | + * to have it redelivered.</li> |
| 739 | + * </ul> |
| 740 | + * <p> |
| 741 | + * This method cannot be used to receive {@code Message} or |
| 742 | + * {@code StreamMessage} objects; a {@code MessageFormatException} will |
| 743 | + * always be thrown for these types. |
| 744 | + * |
| 745 | + * @param c the type to which the body of the next message should be |
| 746 | + * assigned |
| 747 | + * @return the body of the next message, or null if this message consumer |
| 748 | + * is concurrently closed |
| 749 | + * @throws MessageFormatException if the message body cannot be assigned to |
| 750 | + * the specified type, or if the message is a {@code Message} or |
| 751 | + * {@code StreamMessage} |
| 752 | + * @throws JMSException if the JMS provider fails to receive the next |
| 753 | + * message due to some internal error |
| 754 | + */ |
| 755 | + public <T> T receiveBody(Class<T> c) throws JMSException { |
| 756 | + checkClosed(); |
| 757 | + checkMessageListener(); |
| 758 | + |
| 759 | + sendPullCommand(0); |
| 760 | + MessageDispatch md = dequeue(-1); |
| 761 | + if (md == null) { |
| 762 | + return null; |
| 763 | + } |
| 764 | + |
| 765 | + return doReceiveBody(md, c); |
| 766 | + } |
| 767 | + |
| 768 | + /** |
| 769 | + * Receives the next message produced for this message consumer and returns |
| 770 | + * its body as an object of the specified type, blocking up to the |
| 771 | + * specified timeout. A {@code timeout} of zero never expires and the call |
| 772 | + * blocks indefinitely. |
| 773 | + * <p> |
| 774 | + * If the message is not of a type for which the body can be assigned to |
| 775 | + * the specified type, a {@code MessageFormatException} is thrown. The |
| 776 | + * subsequent behaviour depends on the session's acknowledge mode: |
| 777 | + * <ul> |
| 778 | + * <li>{@code AUTO_ACKNOWLEDGE} / {@code DUPS_OK_ACKNOWLEDGE}: the |
| 779 | + * message is not acknowledged and will be delivered again before any |
| 780 | + * subsequent messages. This is not considered redelivery and does not |
| 781 | + * cause the {@code JMSRedelivered} header or |
| 782 | + * {@code JMSXDeliveryCount} property to be updated.</li> |
| 783 | + * <li>{@code CLIENT_ACKNOWLEDGE}: the message is treated as delivered. |
| 784 | + * The application must call {@code session.recover()} to have it |
| 785 | + * redelivered.</li> |
| 786 | + * <li>Transacted session: the message is treated as delivered within the |
| 787 | + * transaction. The application must call {@code session.rollback()} |
| 788 | + * to have it redelivered.</li> |
| 789 | + * </ul> |
| 790 | + * <p> |
| 791 | + * This method cannot be used to receive {@code Message} or |
| 792 | + * {@code StreamMessage} objects; a {@code MessageFormatException} will |
| 793 | + * always be thrown for these types. |
| 794 | + * |
| 795 | + * @param c the type to which the body of the next message should be |
| 796 | + * assigned |
| 797 | + * @param timeout the timeout value (in milliseconds), a timeout of zero |
| 798 | + * never expires |
| 799 | + * @return the body of the next message, or null if the timeout expires or |
| 800 | + * this message consumer is concurrently closed |
| 801 | + * @throws MessageFormatException if the message body cannot be assigned to |
| 802 | + * the specified type, or if the message is a {@code Message} or |
| 803 | + * {@code StreamMessage} |
| 804 | + * @throws JMSException if the JMS provider fails to receive the next |
| 805 | + * message due to some internal error |
| 806 | + */ |
| 807 | + public <T> T receiveBody(Class<T> c, long timeout) throws JMSException { |
| 808 | + checkClosed(); |
| 809 | + checkMessageListener(); |
| 810 | + if (timeout == 0) { |
| 811 | + return this.receiveBody(c); |
| 812 | + } |
| 813 | + |
| 814 | + sendPullCommand(timeout); |
| 815 | + while (timeout > 0) { |
| 816 | + MessageDispatch md; |
| 817 | + if (info.getPrefetchSize() == 0) { |
| 818 | + md = dequeue(-1); |
| 819 | + } else { |
| 820 | + md = dequeue(timeout); |
| 821 | + } |
| 822 | + |
| 823 | + if (md == null) { |
| 824 | + return null; |
| 825 | + } |
| 826 | + |
| 827 | + return doReceiveBody(md, c); |
| 828 | + } |
| 829 | + return null; |
| 830 | + } |
| 831 | + |
| 832 | + /** |
| 833 | + * Receives the next message produced for this message consumer and returns |
| 834 | + * its body as an object of the specified type if one is immediately |
| 835 | + * available. |
| 836 | + * <p> |
| 837 | + * If the message is not of a type for which the body can be assigned to |
| 838 | + * the specified type, a {@code MessageFormatException} is thrown. The |
| 839 | + * subsequent behaviour depends on the session's acknowledge mode: |
| 840 | + * <ul> |
| 841 | + * <li>{@code AUTO_ACKNOWLEDGE} / {@code DUPS_OK_ACKNOWLEDGE}: the |
| 842 | + * message is not acknowledged and will be delivered again before any |
| 843 | + * subsequent messages. This is not considered redelivery and does not |
| 844 | + * cause the {@code JMSRedelivered} header or |
| 845 | + * {@code JMSXDeliveryCount} property to be updated.</li> |
| 846 | + * <li>{@code CLIENT_ACKNOWLEDGE}: the message is treated as delivered. |
| 847 | + * The application must call {@code session.recover()} to have it |
| 848 | + * redelivered.</li> |
| 849 | + * <li>Transacted session: the message is treated as delivered within the |
| 850 | + * transaction. The application must call {@code session.rollback()} |
| 851 | + * to have it redelivered.</li> |
| 852 | + * </ul> |
| 853 | + * <p> |
| 854 | + * This method cannot be used to receive {@code Message} or |
| 855 | + * {@code StreamMessage} objects; a {@code MessageFormatException} will |
| 856 | + * always be thrown for these types. |
| 857 | + * |
| 858 | + * @param c the type to which the body of the next message should be |
| 859 | + * assigned |
| 860 | + * @return the body of the next message, or null if one is not immediately |
| 861 | + * available |
| 862 | + * @throws MessageFormatException if the message body cannot be assigned to |
| 863 | + * the specified type, or if the message is a {@code Message} or |
| 864 | + * {@code StreamMessage} |
| 865 | + * @throws JMSException if the JMS provider fails to receive the next |
| 866 | + * message due to some internal error |
| 867 | + */ |
| 868 | + public <T> T receiveBodyNoWait(Class<T> c) throws JMSException { |
| 869 | + checkClosed(); |
| 870 | + checkMessageListener(); |
| 871 | + sendPullCommand(-1); |
| 872 | + |
| 873 | + MessageDispatch md; |
| 874 | + if (info.getPrefetchSize() == 0) { |
| 875 | + md = dequeue(-1); |
| 876 | + } else { |
| 877 | + md = dequeue(0); |
| 878 | + } |
| 879 | + |
| 880 | + if (md == null) { |
| 881 | + return null; |
| 882 | + } |
| 883 | + |
| 884 | + return doReceiveBody(md, c); |
| 885 | + } |
| 886 | + |
| 887 | + /** |
| 888 | + * Checks that the message body can be assigned to the requested type, |
| 889 | + * acknowledges the message, and returns its body. If the body cannot be |
| 890 | + * assigned, the handling depends on the session's acknowledge mode: |
| 891 | + * <ul> |
| 892 | + * <li>AUTO_ACKNOWLEDGE / DUPS_OK_ACKNOWLEDGE: the message is re-enqueued |
| 893 | + * without acknowledgement so that it remains available for a subsequent |
| 894 | + * {@code receive} or {@code receiveBody} call.</li> |
| 895 | + * <li>CLIENT_ACKNOWLEDGE / TRANSACTED: the message is treated as delivered |
| 896 | + * (not re-enqueued). The application may call {@code session.recover()} |
| 897 | + * or {@code session.rollback()} respectively to redeliver.</li> |
| 898 | + * </ul> |
| 899 | + * <p> |
| 900 | + * Per Jakarta Messaging 3.1, {@code receiveBody} must always throw |
| 901 | + * {@code MessageFormatException} for plain {@code Message} and |
| 902 | + * {@code StreamMessage} types, regardless of what |
| 903 | + * {@code isBodyAssignableTo} returns. |
| 904 | + */ |
| 905 | + private <T> T doReceiveBody(MessageDispatch md, Class<T> c) throws JMSException { |
| 906 | + ActiveMQMessage message = createActiveMQMessage(md); |
| 907 | + |
| 908 | + // Jakarta Messaging 3.1: receiveBody must always fail for Message and StreamMessage. |
| 909 | + // Note: Message.isBodyAssignableTo() returns true for any type per spec, |
| 910 | + // which conflicts with receiveBody's requirement, so we must check explicitly. |
| 911 | + boolean bodyNotAssignable = message.getClass() == ActiveMQMessage.class |
| 912 | + || message instanceof ActiveMQStreamMessage |
| 913 | + || !message.isBodyAssignableTo(c); |
| 914 | + |
| 915 | + if (bodyNotAssignable) { |
| 916 | + if (session.isAutoAcknowledge() || session.isDupsOkAcknowledge()) { |
| 917 | + // re-enqueue for redelivery on next receive/receiveBody call |
| 918 | + unconsumedMessages.enqueueFirst(md); |
| 919 | + } else { |
| 920 | + // CLIENT_ACKNOWLEDGE or TRANSACTED: message is considered delivered, |
| 921 | + // application must use session.recover() or session.rollback() |
| 922 | + beforeMessageIsConsumed(md); |
| 923 | + afterMessageIsConsumed(md, false); |
| 924 | + } |
| 925 | + throw new MessageFormatException("Message body cannot be read as type: " + c); |
| 926 | + } |
| 927 | + |
| 928 | + beforeMessageIsConsumed(md); |
| 929 | + afterMessageIsConsumed(md, false); |
| 930 | + return message.getBody(c); |
| 931 | + } |
| 932 | + |
716 | 933 | /** |
717 | 934 | * Closes the message consumer. |
718 | 935 | * <P> |
|
0 commit comments