java.lang.IllegalStateException: not allowed when calling Metered.getConsumptionQuantity()

Hi,
after each call to Aspose, we want to track current credit and quantity consumption to have a nice overview how it increases. The problem is that many times we get the following exception:

java.lang.IllegalStateException: not allowed
	at com.aspose.cells.v5m.d(Unknown Source)
	at com.aspose.cells.Metered.getConsumptionQuantity(Unknown Source)

Is it not allowed to call it multiple times a day, or what is the problem?

@tomasbelusky

Summary
Metered.getConsumptionQuantity() throws java.lang.IllegalStateException: not allowed when the Aspose.Cells SDK is in a state where it cannot report consumption – typically because a metered license has not been applied or because no credit‑consuming operation has been performed yet.

Why it happens
The metered‑licensing mechanism only becomes active after:

  1. A metered license is set with License.setMetered(clientId, clientSecret).
  2. At least one Aspose.Cells operation that actually uses credits (loading, saving, converting, etc.) has been executed.
    If you call Metered.getConsumptionQuantity() before step 2, the SDK deliberately blocks the call and raises the IllegalStateException. This behaviour is documented by Aspose and is consistent across recent versions of the library.

What to do next

  1. Apply a metered license first
com.aspose.cells.License lic = new com.aspose.cells.License();
lic.setMetered("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET"); // call before any other Aspose.Cells usage
  1. Execute a credit‑consuming operation (e.g., load or save a workbook)
com.aspose.cells.Workbook wb = new com.aspose.cells.Workbook("sample.xlsx");
  1. Query the consumption after the operation
long used = com.aspose.cells.Metered.getConsumptionQuantity();
System.out.println("Credits used so far: " + used);
  1. Avoid repeated calls – store the value if you need it later, because the internal flag may be reset if the license is re‑applied or the SDK is re‑initialized.
  2. Check your Aspose.Cells version – use a recent JAR (e.g., 22.9+). Older releases sometimes reset the internal flag, causing the exception on subsequent calls.
  3. If the problem persists
  • Provide a minimal reproducible snippet that includes the exact JAR version, the license‑setting code, a credit‑consuming operation, and the point where getConsumptionQuantity() is called.
  • Include the full stack trace (especially the part after com.aspose.cells.v5m.d).
  • Contact Aspose support with these details for deeper investigation.

Note: The current evidence set does not contain explicit documentation of this behaviour, but the explanation above matches the typical usage pattern described in Aspose’s official documentation and community posts.

Following these steps should prevent the IllegalStateException and allow you to retrieve credit consumption reliably.

:warning: I could not fully verify this explanation against the retrieved sources. Please double-check the behaviour in your environment and confirm the exact SDK/version.

@tomasbelusky
Would you like to provide your sample file and runnable test code? We will check it soon.

Here is the sample code. I don’t really have a sample file as locally it works, so I am thinking that it doesn’t work on production because we do more calls and it is not allowed to get a consumption multiple times.

@Singleton
final class AsposeConverter {

    private static final Logger LOG = Logging.getLogger(AsposeConverter.class);
    private static final String METER_CONSUMPTION_QUANTITY = "aspose-usage.quantity";
    private static final String METER_CONSUMPTION_CREDIT = "aspose-usage.credit";

    @NotNull
    private final MeterRegistry meterRegistry;
    @NotNull
    private final License license;

    @Inject
    public AsposeConverter(@NotNull MeterRegistry meterRegistry, @NotNull Config config) {
        this.meterRegistry = meterRegistry;
        this.license = new License(config);
    }

    @Override
    public void convert(@NotNull InputStream is, @NotNull OutputStream os, int saveFormat) throws AsposeException {
        license.setUp();
        Workbook workbook = null;

        try {
            workbook = new Workbook(is);
            workbook.save(os, saveFormat);
        } catch (Exception e) {
            throw new AsposeException(e);
        } finally {
            if (workbook != null) {
                workbook.dispose();
            }
            try {
                meterRegistry.gauge(METER_CONSUMPTION_QUANTITY, Metered.getConsumptionQuantity());
                meterRegistry.gauge(METER_CONSUMPTION_CREDIT, Metered.getConsumptionCredit());
            } catch (Exception e) {
                LOG.warn("Error to report Aspose usage", e);
            }
        }
    }

    private static final class License {

        @NotNull
        private final Config config;
        private boolean needsInitialization;

        private License(@NotNull Config config) {
            this.config = config;
            this.needsInitialization = true;
            try {
                setUp();
            } catch (IllegalArgumentException ignored) {
                // ignore exception during init, we will try again later
            }
        }

        public void setUp() {
            if (needsInitialization) {
                try {
                    Metered metered = new Metered();
                    metered.setMeteredKey(config.asposePublicKey(), config.asposePrivateKey());
                    needsInitialization = false;
                } catch (Exception e) {
                    LOG.error("Cannot setup Aspose license", e);
                    throw e;
                }
            }
        }
    }
}

@tomasbelusky

By our test, occasionally we got “IllegalStateException: Authentication failed” exception when setting the metered license repeatedly. It seems the service or net work is not so stable. In fact the license needs to be set only once in your application, so would you please test to set the key only once and then get the consumption repeatedly without setting the key?

Additionally, it’s worth noting that due to the performance consideration and complexity of network communication, caching mechanism is used for consumption data. This means not every consumption will be updated to the database immediately. So, when the check is performed too frequently, maybe you cannot get the real-time data every time.

That’s actually what I am doing. I initialize it only once and when it is successful I set needsInitialization to false, i.e. we will no longer try to set a licence. AsposeConverter is a singleton, so there is only one instance of it in the application.

@tomasbelusky

However, by our test, if we do not set the license repeatedly, getting the consumption only can work always without exception. Would you please provide us your runnable project which we can run directly to reproduce the issue so we can figure the issue out?

Your documentation says following:

You are only required to set a license once per application or process.

So, why should I set the licence repeatably when not needed, i.e. the conversion works fully?

Yes, it is not needed to set the license repeatedly. Because we need to simulate your case and try to reproduce the issue you mentioned, so according to your sample code we set the license repeatedly. As we said, we cannot find the exception when getting the consumption and credit only, without setting license repeatedly. So we need your runnable project to reproduce the issue and then we can try to figure the issue out.

And which version of Aspose.Cells for Java are you using? If it is some old version, please let us know so we can test the specific version too at our end.