Comments (1)
I ran into the same problem and solved it by writing the sitemap contents to strings and check the length of these strings. If any of the generated lists exceeds 10Mb then I decreased the number of max URLs and started all over again. This might take up some time but if you have a chance to test locally with your production data you can find a better starting value for maxUrls
which boosts the performance of the while
loop for your production code. In our case it took at most a second or third try to find a better value for maxUrls
.
private final Map<String, Pair<Date, String>> sitemaps = new HashMap<>();
private final AtomicInteger maxUrls = new AtomicInteger(50000);
private void generateGoogleImageSitemapContents(String fileNamePrefix, Function<Integer, GoogleImageSitemapGenerator> sitemapCreator) {
Predicate<GoogleImageSitemapGenerator> exceeds10Mb = googleImageSitemapGenerator -> googleImageSitemapGenerator.writeAsStrings()
.stream()
.anyMatch(list -> list.getBytes(US_ASCII).length > 1e+7);
GoogleImageSitemapGenerator generator = sitemapCreator.apply(maxUrls.get());
if (generator == null) {
return;
}
while (exceeds10Mb.test(generator) && maxUrls.get() > 0) {
generator = sitemapCreator.apply(maxUrls.addAndGet(-maxUrls.get() / 2));
}
// In theory now all lists are smaller than 10Mb lets store them for later
AtomicInteger counter = new AtomicInteger(0);
generator.writeAsStrings()
.forEach(sitemapContent -> sitemaps.put(fileNamePrefix + counter.getAndIncrement() + ".xml", Pair.of(new Date(), sitemapContent)));
}
And later you can use this method like this
generateGoogleImageSitemapContents("changes_daily_with_images_", maxUrls -> {
try {
GoogleImageSitemapGenerator imageGenerator = GoogleImageSitemapGenerator.builder(baseUrl, null)
.dateFormat(new W3CDateFormat(MINUTE))
.maxUrls(maxUrls)
.build();
addProducts(imageGenerator);
addCategories(imageGenerator);
return imageGenerator;
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
});
In my case I store the sitemaps in-memory and deliver them on request. The Pair of date and sitemap content is used to add a <lastMod/>
-Tag to each list. The lists are created before they are queried and are ready if the crawler requests them. This way it is also possible to update the list in background and use Google-Ping-Service to inform the crawler about changes.
from sitemapgen4j.
Related Issues (20)
- error with gzip(true) and autovalidate(true) HOT 1
- Error using maxUrls( .. )
- Allow Empty Files HOT 1
- Thread safety HOT 1
- Escape XML entities HOT 12
- Publish license HOT 2
- xhtml:link support HOT 6
- Pagination Support
- Writing in static/sitemap.xml HOT 2
- sitemapgen4j as Maven dependency HOT 2
- Accessing ISitemapUrl interface throws IllegalAccessError HOT 3
- Add A License HOT 3
- writeSitemapsWithIndex() should return a File, like write() HOT 1
- Write to a Writer instead of to a file. HOT 5
- Images in web sitemap HOT 3
- Autovalidation for GoogleImageSitemapGenerator throws RuntimeException
- Missing documentation
- Can't set restriction
- Possibility to customize the name of sitemap_index.xml in Method com.redfin.sitemapgenerator.SitemapGenerator.writeSitemapsWithIndex() HOT 2
- Add image to GoogleLingSitemapUrl
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sitemapgen4j.